<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Using CNN to extract data from plots
We'll start by making synthetic images of plots that look like "real" log plots
Train a 5-6 layer CNN using Keras
Return 20 inferred points from the RGB image fed. (the idea is to segment the log in chunks)
Repeat until it works
TODO
Step1:  Synthetic image generation function
Step2:  Batch image generation function
Step3:  Let's start with 1000 training samples and 100 test samples
Step4:  We reshaped the images to be squared just because it's easier. Here's how they look
Step5:  CNN
Let's implement CNN's until they work!
Step6:  Train
Step7:  Previous run took ~10mins
Results so far...
Step8:  Not bad at all!
For our last trick before we run out of time, a real image!
Step9:  Trying to lineup things
<ASSISTANT_TASK:>
Python Code:
import pylab as plt
# %matplotlib inline
import numpy as np
Explanation: Using CNN to extract data from plots
We'll start by making synthetic images of plots that look like "real" log plots
Train a 5-6 layer CNN using Keras
Return 20 inferred points from the RGB image fed. (the idea is to segment the log in chunks)
Repeat until it works
TODO:
- Use OCR output to scale points extracted to physical units
- Expand the amount of points extracted (from 20 to 100s?)
- Test/Generate other types of log plots.
End of explanation
def gen_im(n=1):
    
    rn = np.random.uniform(low=0, high=1, size=(20,))
    # Make a random plot...
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.plot(rn,"r")
    ax.grid()
    
    DPI = fig.get_dpi()
    fig.set_size_inches(150/float(DPI),150/float(DPI))
#     fig.set_size_inches(3,3)
    fig.canvas.draw()
    # Now we can save it to a numpy array.
    data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
#     data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
    
    plt.close('all')
    return data, rn, fig.canvas.get_width_height()[::-1]
_,_,size = gen_im(n=1)
Explanation: Synthetic image generation function:
End of explanation
def batch_gen(n=1):
    for i in range(n):
        if i==0:
            arr, vals, size = gen_im()
        else:
            tmparr, tmpvals, size = gen_im()
            arr = np.vstack((arr,tmparr))
            vals = np.vstack((vals,tmpvals))
            
    arr = arr.reshape(arr.shape[0], *size, 3)[:,:,:,:]
    
    return arr, vals
Explanation: Batch image generation function:
End of explanation
x_train, y_train = batch_gen(n=1000)
x_test, y_test = batch_gen(n=100)
Explanation: Let's start with 1000 training samples and 100 test samples
End of explanation
plt.imshow(x_train[0,:].reshape(*size,3))
plt.show()
Explanation: We reshaped the images to be squared just because it's easier. Here's how they look:
End of explanation
import keras
from keras.layers import Dense, Flatten, Dropout, Conv2D, MaxPooling2D
from keras.models import Sequential
from keras.layers.convolutional import ZeroPadding2D
from keras.layers import Activation
from keras.optimizers import SGD
import matplotlib.pylab as plt
batch_size = 1
num_classes = 20
epochs = 10
# input image dimensions
img_x, img_y = size
input_shape = (img_x, img_y, 3)
print('x_train shape:', x_train.shape)
filter_size = 3
pool_size = 2
model = Sequential([
        Conv2D(32, (6, 6), input_shape=input_shape, data_format="channels_last", activation='relu'), 
        MaxPooling2D(pool_size=(pool_size, pool_size)), 
        Conv2D(64, (filter_size, filter_size), data_format="channels_last", activation='relu'), 
        MaxPooling2D(pool_size=(pool_size, pool_size)), 
        Conv2D(128, (filter_size, filter_size), data_format="channels_last", activation='relu'), 
# #         MaxPooling2D(pool_size=(pool_size, pool_size)), 
        Conv2D(128, (filter_size, filter_size), data_format="channels_last", activation='relu'), 
# #         MaxPooling2D(pool_size=(pool_size, pool_size)), 
        Flatten(), 
#         Dropout(0.4), 
#         Dense(1000, activation='relu'), 
        Dropout(0.4), 
        Dense(num_classes, activation='linear'),
    ])
model.compile('adadelta', 'mse')
Explanation: CNN
Let's implement CNN's until they work!
End of explanation
model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          verbose=1,
          validation_data=(x_test, y_test),
          callbacks=[history])
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score)
Explanation: Train
End of explanation
model.predict(x_test[0].reshape(1,*size, 3))
plt.plot(model.predict(x_test)[0])
plt.plot(y_test[0])
plt.show()
Explanation: Previous run took ~10mins
Results so far...
End of explanation
from PIL import Image
im = Image.open("log_crop_smaller.png")
im
im = im.resize((150,150)).rotate(90)
im
im_arr = np.array(im.getdata())[:,:-1].reshape(1, im.size[1], im.size[0], 3)
plt.plot(model.predict(im_arr)[0])
plt.show()
Explanation: Not bad at all!
For our last trick before we run out of time, a real image!
End of explanation
fig, (ax0, ax1) = plt.subplots(nrows=2, sharex=True)
ax0.imshow(im, extent=(-2, 19, 0, 1), aspect=4)
ax0.set_title('Real Log')
ax0.set_xlim(-3,20)
ax1.plot(model.predict(im_arr)[0],'r')
ax1.set_title('Predicted Log')
ax1.set_ylim(0,1)
plt.show()
Explanation: Trying to lineup things:
End of explanation
<END_TASK>