<jupyter_start><jupyter_text>1) Data Access<jupyter_code>#Read Training data and convert into Lab space
#To convert images to 256x256 use this command on the bash
#for i in *.jpg; do convert $i -scale 256x256 -gravity center -background white -extent 256x256 resized/f$i; don
#Read Training data
X = []
for filename in os.listdir('mixed/'):    
    X.append(img_to_array(load_img('mixed/'+filename)))
X = np.array(X, dtype=float)
Xtrain = 1.0/255*X
#Convert into Lab space
for i in np.arange(len(Xtrain)):
    Xtrain[i] = rgb2lab(Xtrain[i])
    Xtrain[i] = (Xtrain[i] + [0, 128, 128]) / [100, 255, 255]
Xtrain.shape
#Separate X (Lightness) and Y(ab images)
Y_train = Xtrain[:,:,:,1:]
X_train = Xtrain[:,:,:,0]
X_train=X_train.reshape((X_train.shape[0],X_train.shape[1],X_train.shape[2],1))
#Read Testing data
X_test = []
for filename in os.listdir('test'):    
    X_test.append(img_to_array(load_img('test/'+filename))/255.)
X_test = np.array(X_test)<jupyter_output><empty_output><jupyter_text>2) Model Building<jupyter_code>#Encoder
encoder_input = Input(shape=(256, 256, 1,))
encoder_output = Conv2D(64, (3,3), activation='relu', padding='same', strides=2)(encoder_input)
encoder_output = Conv2D(128, (3,3), activation='relu', padding='same')(encoder_output)
encoder_output = Conv2D(128, (3,3), activation='relu', padding='same', strides=2)(encoder_output)
encoder_output = Conv2D(256, (3,3), activation='relu', padding='same')(encoder_output)
encoder_output = Conv2D(256, (3,3), activation='relu', padding='same', strides=2)(encoder_output)

#Decoder
decoder_output = Conv2D(128, (3,3), activation='relu', padding='same')(encoder_output)
decoder_output = UpSampling2D((2, 2))(decoder_output)
decoder_output = Conv2D(64, (3,3), activation='relu', padding='same')(decoder_output)
decoder_output = UpSampling2D((2, 2))(decoder_output)
decoder_output = Conv2D(32, (3,3), activation='relu', padding='same')(decoder_output)
decoder_output = Conv2D(16, (3,3), activation='relu', padding='same')(decoder_output)
decoder_output = Conv2D(2, (3, 3), activation='tanh', padding='same')(decoder_output)
decoder_output = UpSampling2D((2, 2))(decoder_output)

model = Model(inputs=encoder_input, outputs=decoder_output)

model.summary()
batch_size = 20
epochs = 30
#Train model      
#tensorboard = TensorBoard(log_dir="./output")
start_time = time.time()
model.compile(optimizer='adam', loss='mse')
history = model.fit(x=X_train,y=Y_train,batch_size=batch_size,epochs=epochs,
                    validation_split=0.10,shuffle=True)
print('Finished Training')
print("--- %s seconds ---" % (time.time() - start_time))
def plot_curves(history):
    loss = history.history['loss']
    val_loss=history.history['val_loss']
    epochs = range(1, len(loss)+1)
    with plt.style.context('fivethirtyeight'):
        plt.figure(figsize=(20,10))
        plt.plot(epochs, loss, label='Training loss')
        plt.plot(epochs, val_loss, label='Validation loss')
        plt.legend(frameon=False)
        plt.show()
    
plot_curves(history)
def colour_image(image_data):
    input_img = rgb2lab(image_data)
    #input_img = rgb2gray(image_data)
    input_img = (input_img + [0, 128, 128]) / [100, 255, 255]
    input_img = input_img[:,:,0]
    input_img = input_img.reshape((1,256,256,1))
    imgout = model.predict(input_img)
    coloured = np.zeros((256,256,3))
    coloured[:,:,0] = input_img.reshape((256,256))
    coloured[:,:,1:] = imgout
    coloured = (coloured * [100, 255, 255]) - [0, 128, 128]
    rgb_image = lab2rgb(coloured)
    plt.figure(figsize=(20,10))
    plt.subplot(121)
    plt.imshow(rgb_image)
    plt.subplot(132)
    plt.imshow(image_data)
    plt.subplot(133)
    plt.imshow(rgb_image)
    
for i in np.arange(20,30):
    image_number=i
    colour_image(X_test[image_number])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16666666666666666, 'fraction_numerical': 0.05590551181102362, 'mean_word_length': 5.616319444444445, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '542932', 'license': 'MIT', 'n_tokens_mistral': 1566, 'n_tokens_neox': 1413, 'n_words': 260, 'path': 'keras.ipynb', 'repo_name': 'umairacheema/keras-pytorch-equivalence', 'size': '3810'}