Skip to content

Instantly share code, notes, and snippets.

@neilslater
Created August 9, 2016 07:58
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save neilslater/40201a6c63b4462e6c6e458bab60d0b4 to your computer and use it in GitHub Desktop.
Save neilslater/40201a6c63b4462e6c6e458bab60d0b4 to your computer and use it in GitHub Desktop.
Keras example image regression, extract texture height param
# -*- coding: utf-8 -*-
import numpy as np
import os
import cv2
import pandas as pd
from sklearn.cross_validation import train_test_split
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D
from keras.optimizers import RMSprop, Adam, Adadelta
image_size = 50
def load_train():
X_train = []
y_train = []
heights = pd.read_csv('heights.csv')
print('Read train images')
for index, row in heights.iterrows():
image_path = os.path.join('images', 'train', str(int(row['img'])) + '.png')
img = cv2.resize(cv2.imread(image_path, cv2.CV_LOAD_IMAGE_COLOR), (image_size, image_size) ).astype(np.float32)
img = img.transpose((2,0,1))
X_train.append(img)
y_train.append( [ row['height'] ] )
return X_train, y_train
def read_and_normalize_train_data():
train_data, train_target = load_train()
train_data = np.array(train_data, dtype=np.float32)
train_target = np.array(train_target, dtype=np.float32)
m = train_data.mean()
s = train_data.std()
print ('Train mean, sd:', m, s )
train_data -= m
train_data /= s
print('Train shape:', train_data.shape)
print(train_data.shape[0], 'train samples')
return train_data, train_target
def create_model():
nb_filters = 8
nb_conv = 5
model = Sequential()
model.add(Convolution2D(nb_filters, nb_conv, nb_conv,
border_mode='valid',
input_shape=(3, image_size, image_size) ) )
model.add(Activation('relu'))
model.add(Convolution2D(nb_filters, nb_conv, nb_conv))
model.add(Activation('relu'))
model.add(Convolution2D(nb_filters, nb_conv, nb_conv))
model.add(Activation('relu'))
model.add(Convolution2D(nb_filters, nb_conv, nb_conv))
model.add(Activation('relu'))
model.add(Dropout(0.25))
model.add(Convolution2D(nb_filters*2, nb_conv, nb_conv))
model.add(Activation('relu'))
model.add(Convolution2D(nb_filters*2, nb_conv, nb_conv))
model.add(Activation('relu'))
model.add(Convolution2D(nb_filters*2, nb_conv, nb_conv))
model.add(Activation('relu'))
model.add(Convolution2D(nb_filters*2, nb_conv, nb_conv))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(256))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('linear'))
model.compile(loss='mean_squared_error', optimizer=Adadelta())
return model
def train_model(batch_size = 50, nb_epoch = 20):
num_samples = 1999
cv_size = 499
train_data, train_target = read_and_normalize_train_data()
train_data = train_data[0:num_samples,:,:,:]
train_target = train_target[0:num_samples]
X_train, X_valid, y_train, y_valid = train_test_split(train_data, train_target, test_size=cv_size, random_state=56741)
model = create_model()
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, validation_data=(X_valid, y_valid) )
predictions_valid = model.predict(X_valid, batch_size=50, verbose=1)
compare = pd.DataFrame(data={'original':y_valid.reshape((cv_size,)),
'prediction':predictions_valid.reshape((cv_size,))})
compare.to_csv('compare.csv')
return model
train_model(nb_epoch = 50)
@neilslater
Copy link
Author

neilslater commented Aug 9, 2016

The accuracy on this version is not great (loss around 0.0005 - it could probably be better with more training examples), however it doesn't suffer from the "everything is the mean value" problem seen when training on the raw 100x100 images. The output file compare.csv gives actual vs predicted for cross-validation set. Format of height file is csv with headers "img" and "height". The img column is an integer referencing image stored in images/train/ directory relative to script.

@fvisconti
Copy link

Is line 23: img = img.transpose((2,0,1)) there to put the channel first (Theano format)?

@bemoregt
Copy link

Hi, @neilslater

I made csv file like this

img, height
0.3, 0.3
0.5, 0.5
0.7, 0.7
0.8, 0.8
0.9, 0.9

but I have met this error:

kerb:deepRegress mun$ python3 brix.py
/usr/local/lib/python3.6/site-packages/sklearn/cross_validation.py:41: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
"This module will be removed in 0.20.", DeprecationWarning)
Using TensorFlow backend.
Read train images
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 2566, in get_value
return libts.get_value_box(s, key)
File "pandas/_libs/tslib.pyx", line 1017, in pandas._libs.tslib.get_value_box
File "pandas/_libs/tslib.pyx", line 1025, in pandas._libs.tslib.get_value_box
TypeError: 'str' object cannot be interpreted as an integer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "brix.py", line 111, in
train_model(nb_epoch = 5)
File "brix.py", line 95, in train_model
train_data, train_target = read_and_normalize_train_data()
File "brix.py", line 29, in read_and_normalize_train_data
train_data, train_target = load_train()
File "brix.py", line 25, in load_train
y_train.append( [ row['height'] ] )
File "/usr/local/lib/python3.6/site-packages/pandas/core/series.py", line 623, in getitem
result = self.index.get_value(self, key)
File "/usr/local/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 2574, in get_value
raise e1
File "/usr/local/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 2560, in get_value
tz=getattr(series.dtype, 'tz', None))
File "pandas/_libs/index.pyx", line 83, in pandas._libs.index.IndexEngine.get_value
File "pandas/_libs/index.pyx", line 91, in pandas._libs.index.IndexEngine.get_value
File "pandas/_libs/index.pyx", line 139, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1265, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1273, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'height'

What's wrong to me?

from @bemoregt.

@AzureIP
Copy link

AzureIP commented Jul 19, 2018

I think it is trying to read some image number from the csv. So if you have 678.png as file there has to be some 678 in the csv.
I am still not fully completed in figuring out how the file shall look like, abut once finished, I'll post an example. Or could someone else be so friendly and do so?

@AzureIP
Copy link

AzureIP commented Jul 19, 2018

img,height
678,227
Is working

@AzureIP
Copy link

AzureIP commented Jul 19, 2018

@fvisconti Maybe to shift a 227x227x3 tensor to a 3x227x227 tensor for some reason?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment