<jupyter_start><jupyter_text>Author: <NAME> | See full post at 3blades  Pixel Regression.Can we recover an image by learning a deep regression map from pixels $(x,y)$ to colors $(r,g,b)$?Our target image will be Mona Lisa:<jupyter_code>import matplotlib.image as mpimg
import matplotlib.pylab as plt
import numpy as np
%matplotlib inline

im = mpimg.imread("data/monalisa.jpg")

plt.imshow(im)
plt.show()
im.shape<jupyter_output><empty_output><jupyter_text>Our training dataset will be composed of pixels locations and input and pixel values as output:<jupyter_code>X_train = []
Y_train = []
for i in range(im.shape[0]):
    for j in range(im.shape[1]):
        X_train.append([float(i),float(j)])
        Y_train.append(im[i][j])
        
X_train = np.array(X_train)
Y_train = np.array(Y_train)
print 'Samples:', X_train.shape[0]
print '(x,y):', X_train[0],'\n', '(r,g,b):',Y_train[0]<jupyter_output>Samples: 30447
(x,y): [ 0.  0.] 
(r,g,b): [ 85 105 116]<jupyter_text>Our objective is to train a deep MLP that is able to reconstruct the image:![alt text](images/result.png)<jupyter_code>import keras
from keras.models import Sequential
from keras.layers.core import Dense, Activation, Dropout
from keras.optimizers import Adam, RMSprop, Nadam


# Model architecture
model = Sequential()

model.add(Dense(500, input_dim=2, init='uniform'))
model.add(Activation('relu'))

model.add(Dense(500, init='uniform'))
model.add(Activation('relu'))

model.add(Dense(500, init='uniform'))
model.add(Activation('relu'))

model.add(Dense(500, init='uniform'))
model.add(Activation('relu'))

model.add(Dense(500, init='uniform'))
model.add(Activation('relu'))

model.add(Dense(3, init='uniform'))
model.add(Activation('linear'))

model.summary()

# Compile model
model.compile(loss='mean_squared_error',
              optimizer=Nadam(),
              metrics=['accuracy'])
# Why use NAdam Optimizer?
# Much like Adam is essentially RMSprop with momentum, Nadam is Adam RMSprop with Nesterov momentum.
# use this cell to find the best model architecture
history = model.fit(X_train, Y_train, nb_epoch=1000, shuffle=True, verbose=1, batch_size=500)
Y = model.predict(X_train, batch_size=10000)
k = 0
im_out = im[:]
for i in range(im.shape[0]):
    for j in range(im.shape[1]):
        im_out[i,j]= Y[k]
        k += 1
        
print "Mona Lisa by DL"
plt.imshow(im_out)
plt.show()
plt.imshow(im_out)
plt.show()
# summarize history for accuracy
plt.plot(history.history['acc'], 'b')
plt.title('Model Accuracy')
plt.xlabel('epoch')
plt.show()
# summarize history for loss
plt.plot(history.history['loss'], 'r')
plt.title('Model Loss')
plt.xlabel('epoch')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15048543689320387, 'fraction_numerical': 0.020537714712471993, 'mean_word_length': 5.378571428571429, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '471968', 'license': 'MIT', 'n_tokens_mistral': 1027, 'n_tokens_neox': 958, 'n_words': 238, 'path': 'Pixel Regression - Step by Step.ipynb', 'repo_name': 'shugert/DeepLearning', 'size': '2686'}