Skip to content

Instantly share code, notes, and snippets.

@harveyslash
Created November 24, 2016 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harveyslash/5c98f9fdab0d53a2a48f477a52d8588d to your computer and use it in GitHub Desktop.
Save harveyslash/5c98f9fdab0d53a2a48f477a52d8588d to your computer and use it in GitHub Desktop.
def shuffle_in_unison(a, b):
assert len(a) == len(b)
shuffled_a = np.empty(a.shape, dtype=a.dtype)
shuffled_b = np.empty(b.shape, dtype=b.dtype)
permutation = np.random.permutation(len(a))
for old_index, new_index in enumerate(permutation):
shuffled_a[new_index] = a[old_index]
shuffled_b[new_index] = b[old_index]
return shuffled_a, shuffled_b
##prepare for calendars
filelist = glob.glob('businessResized/*.jpg')
images = np.zeros([len(filelist), 70,52, 3], dtype=int)
for i in range(0, len(filelist)):
images[i] = misc.imread(filelist[i],mode= 'RGB')
XComplete = images
YComplete = np.zeros(shape=(len(filelist), 1), dtype='int32')
################prepare for business
filelist = glob.glob('horrorResized/*.jpg')
images = np.zeros([len(filelist), 70,52, 3], dtype=int)
for i in range(0, len(filelist)):
images[i] = misc.imread(filelist[i], mode ='RGB')
XComplete = np.concatenate((XComplete, images))
YComplete = np.concatenate((YComplete, np.ones(shape=(len(filelist), 1), dtype='int32')))
YComplete = YComplete.reshape((len(YComplete)))
YComplete = np_utils.to_categorical(YComplete, 2)
print(type(YComplete))
print(YComplete.shape)
print("printing y complete", YComplete)
print(np.unique(YComplete))
XComplete, YComplete = shuffle_in_unison(XComplete, YComplete)
#
# for i in range(10):
# value = random.randint(0, len(YComplete))
# plt.imshow(misc.toimage(XComplete[value]))
# print(YComplete[value])
# plt.show()
x_train, x_test, y_train, y_test = train_test_split(XComplete, YComplete, test_size=0.1)
print(len(x_train))
model = Sequential()
model.add(Convolution2D(32, 5, 5, border_mode='same', input_shape=(70,52, 3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Convolution2D(64, 5, 5, border_mode='same'))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
# model.add(Convolution2D(128, 7, 7, border_mode='same'))
# model.add(Activation("relu"))
# model.add(MaxPooling2D(pool_size=(2,2)))
# model.add(Convolution2D(64, 10, 10, border_mode='same'))
# model.add(Activation("relu"))
# model.add(MaxPooling2D(pool_size=(2,2)))
# model.add(Convolution2D(16, 8, 8, border_mode='same'))
# model.add(Activation("relu"))
#
# model.add(Convolution2D(8, 4, 4, border_mode='same'))
# model.add(Activation("relu"))
model.add(Flatten())
model.add(Dense(output_dim=512))
model.add(Activation("relu"))
model.add(Dense(output_dim=2))
model.add(Activation("softmax"))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x_train, y_train, nb_epoch=70, batch_size=500,verbose=1)
score = model.evaluate(x_test, y_test, batch_size=16)
print('Test score:', score[0])
print('Test accuracy:', score[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment