Advertisement
Guest User

Untitled

a guest
Feb 8th, 2017
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.94 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from __future__ import print_function, division
  3.  
  4. from theano.sandbox import cuda
  5. #path = "data/state/sample/"
  6. import utils_console; reload(utils_console)
  7. from utils_console import *
  8. from IPython.display import FileLink
  9.  
  10. cuda.use('gpu0')
  11.  
  12. # path = "data/state.tiny/"
  13. path = "data/state/"
  14.  
  15. # batch_size=16
  16. # batch_size=64
  17. batch_size=48
  18.  
  19.  
  20. # (val_classes, trn_classes, val_labels, trn_labels, val_filenames, filenames, test_filenames) = get_classes(path)
  21.  
  22. # Rather than using batches, we could just import all the data into an array to save some processing time.
  23. # (In most examples I'm using the batches, however - just because that's how I happened to start out.)
  24.  
  25. ########################################
  26. #exit();
  27. ########################################
  28.  
  29. _="""
  30. trn = get_data(path+'train')
  31. val = get_data(path+'valid')
  32.  
  33. save_array(path+'results/val.dat', val)
  34. save_array(path+'results/trn.dat', trn)
  35.  
  36. val = load_array(path+'results/val.dat')
  37. trn = load_array(path+'results/trn.dat')
  38. """
  39.  
  40. # Since we have so little data, and it is similar to imagenet images (full color photos), using pre-trained VGG weights is likely to be helpful -
  41. # in fact it seems likely that we won't need to fine-tune the convolutional layer weights much, if at all.
  42. # So we can pre-compute the output of the last convolutional layer, as we did in lesson 3 when we experimented with dropout.
  43. # (However this means that we can't use full data augmentation, since we can't pre-compute something that changes every image.)
  44.  
  45. vgg = Vgg16()
  46. model=vgg.model
  47. last_conv_idx = [i for i,l in enumerate(model.layers) if type(l) is Convolution2D][-1]
  48. conv_layers = model.layers[:last_conv_idx+1]
  49.  
  50. conv_model = Sequential(conv_layers)
  51.  
  52. (val_classes, trn_classes, val_labels, trn_labels, val_filenames, filenames, test_filenames) = get_classes(path)
  53.  
  54. print("Executing: conv_model.predict_generator(batches, batches.nb_sample)")
  55. ###!!!batches = get_batches(path+'train', batch_size=batch_size)
  56. ###!!!conv_feat = conv_model.predict_generator(batches, batches.nb_sample)
  57. ###!!!save_array(path+'results/conv_feat.dat', conv_feat) # conv_feat = load_array(path+'results/conv_feat.dat')
  58. ######!!!
  59. conv_feat = load_array(path+'results/conv_feat.dat')
  60. ######!!!
  61.  
  62. print("Executing: conv_model.predict_generator(val_batches, val_batches.nb_sample)")
  63. ###!!!val_batches = get_batches(path+'valid', batch_size=batch_size*2, shuffle=False)
  64. ###!!!conv_val_feat = conv_model.predict_generator(val_batches, val_batches.nb_sample)
  65. ###!!!save_array(path+'results/conv_val_feat.dat', conv_val_feat) # conv_val_feat = load_array(path+'results/conv_val_feat.dat')
  66. ######!!!
  67. conv_val_feat = load_array(path+'results/conv_val_feat.dat')
  68. ######!!!
  69.  
  70. print("Executing: conv_model.predict_generator(test_batches, test_batches.nb_sample)")
  71. ###!!!test_batches = get_batches(path+'test', batch_size=batch_size)
  72. ###!!!conv_test_feat = conv_model.predict_generator(test_batches, test_batches.nb_sample)
  73. ###!!!save_array(path+'results/conv_test_feat.dat', conv_test_feat) # load_array(path+'results/conv_test_feat.dat')
  74. ######!!!
  75. conv_test_feat = load_array(path+'results/conv_test_feat.dat')
  76. ######!!!
  77.  
  78. # ### Pre-computed data augmentation + dropout
  79. # We'll use our usual data augmentation parameters:
  80.  
  81. gen_t = image.ImageDataGenerator(rotation_range=15, height_shift_range=0.05, shear_range=0.1, channel_shift_range=20, width_shift_range=0.1)
  82. da_batches = get_batches(path+'train', gen_t, batch_size=batch_size, shuffle=False)
  83.  
  84. # We use those to create a dataset of convolutional features 5x bigger than the training set.
  85. ###!!!da_conv_feat = conv_model.predict_generator(da_batches, da_batches.nb_sample*5)
  86. ###!!!save_array(path+'results/da_conv_feat2.dat', da_conv_feat) # da_conv_feat = load_array(path+'results/da_conv_feat2.dat')
  87. ######!!!
  88. ###da_conv_feat = conv_model.predict_generator(da_batches, da_batches.nb_sample*2)
  89. ###save_array(path+'results/da_conv_feat2a.dat', da_conv_feat) # da_conv_feat = load_array(path+'results/da_conv_feat2a.dat')
  90. da_conv_feat = load_array(path+'results/da_conv_feat2a.dat')
  91. ######!!!
  92.  
  93. # Let's include the real training data as well in its non-augmented form.
  94. da_conv_feat = np.concatenate([da_conv_feat, conv_feat])
  95.  
  96. # Since we've now got a dataset 6x bigger than before, we'll need to copy our labels 6 times too.
  97. ###!!!da_trn_labels = np.concatenate([trn_labels]*6)
  98. ######!!!
  99. da_trn_labels = np.concatenate([trn_labels]*3)
  100. ######!!!
  101.  
  102. # Based on some experiments the previous model works well, with bigger dense layers.
  103. def get_bn_da_layers(bn, dense):
  104. return [
  105. MaxPooling2D(input_shape=conv_layers[-1].output_shape[1:]),
  106. Flatten(),
  107. Dropout(bn),
  108. Dense(dense, activation='relu'),
  109. BatchNormalization(),
  110. Dropout(bn),
  111. Dense(dense, activation='relu'),
  112. BatchNormalization(),
  113. Dropout(bn),
  114. Dense(10, activation='softmax')
  115. ]
  116.  
  117. #--------------------------------------------------------------
  118. def fitx(bn, dense, lr, epochs):
  119. print("Fitting: ", bn, dense, lr, epochs)
  120. x_model = Sequential(get_bn_da_layers(bn, dense))
  121. x_model.compile(Adam(lr=lr), loss='categorical_crossentropy', metrics=['accuracy'])
  122. x_model.fit(da_conv_feat, da_trn_labels, batch_size=batch_size, nb_epoch=epochs, validation_data=(conv_val_feat, val_labels))
  123. return x_model
  124. #--------------------------------------------------------------
  125.  
  126. bn_model = fitx(0.6, 640, 0.000004, 12)
  127.  
  128. _="""
  129. bn_model = Sequential(get_bn_da_layers(0.8, 256))
  130. bn_model.compile(Adam(lr=0.001), loss='categorical_crossentropy', metrics=['accuracy'])
  131.  
  132. # Now we can train the model as usual, with pre-computed augmented data.
  133. bn_model.fit(da_conv_feat, da_trn_labels, batch_size=batch_size, nb_epoch=1, validation_data=(conv_val_feat, val_labels))
  134.  
  135. bn_model.optimizer.lr=0.01
  136. bn_model.fit(da_conv_feat, da_trn_labels, batch_size=batch_size, nb_epoch=4, validation_data=(conv_val_feat, val_labels))
  137.  
  138. bn_model.optimizer.lr=0.0001
  139.  
  140. bn_model.fit(da_conv_feat, da_trn_labels, batch_size=batch_size, nb_epoch=4, validation_data=(conv_val_feat, val_labels))
  141. """
  142.  
  143. # Looks good - let's save those weights.
  144. bn_model.save_weights(path+'models/da_conv8_1.h5')
  145.  
  146. _="""
  147. # ### Pseudo labeling
  148. # We're going to try using a combination of [pseudo labeling](http://deeplearning.net/wp-content/uploads/2013/03/pseudo_label_final.pdf) and [knowledge distillation](https://arxiv.org/abs/1503.02531) to allow us to use unlabeled data (i.e. do semi-supervised learning). For our initial experiment we'll use the validation set as the unlabeled data, so that we can see that it is working without using the test set. At a later date we'll try using the test set.
  149. # To do this, we simply calculate the predictions of our model...
  150.  
  151. val_pseudo = bn_model.predict (conv_val_feat, batch_size=batch_size)
  152.  
  153. # ...concatenate them with our training labels...
  154. comb_pseudo = np.concatenate ([da_trn_labels, val_pseudo])
  155. comb_feat = np.concatenate ([da_conv_feat, conv_val_feat])
  156.  
  157. # ...and fine-tune our model using that data.
  158. bn_model.load_weights(path+'models/da_conv8_1.h5')
  159. bn_model.fit(comb_feat, comb_pseudo, batch_size=batch_size, nb_epoch=1, validation_data=(conv_val_feat, val_labels))
  160.  
  161. bn_model.fit(comb_feat, comb_pseudo, batch_size=batch_size, nb_epoch=4, validation_data=(conv_val_feat, val_labels))
  162.  
  163. bn_model.optimizer.lr=0.00001
  164.  
  165. bn_model.fit(comb_feat, comb_pseudo, batch_size=batch_size, nb_epoch=4, validation_data=(conv_val_feat, val_labels))
  166.  
  167. # That's a distinct improvement - even although the validation set isn't very big. This looks encouraging for when we try this on the test set.
  168. bn_model.save_weights(path+'models/bn-ps8.h5')
  169. """
  170.  
  171. # ### Submit
  172.  
  173. # We'll find a good clipping amount using the validation set, prior to submitting.
  174. def do_clip(arr, mx):
  175. return np.clip(arr, (1-mx)/9, mx)
  176.  
  177. ###########################
  178. # Trying to come up with missing 'val_preds'
  179. val_preds = bn_model.predict(conv_val_feat, batch_size=batch_size)
  180. ###########################
  181.  
  182. x = keras.metrics.categorical_crossentropy(val_labels, do_clip(val_preds, 0.93)).eval()
  183.  
  184. # Should be loaded already
  185. # conv_test_feat = load_array(path+'results/conv_test_feat.dat')
  186. pass
  187.  
  188. preds = bn_model.predict(conv_test_feat, batch_size=batch_size*2)
  189.  
  190. # preds # .shape = (46, 10)
  191. # array([[ 0.089 , 0.264 , 0.058 , 0.151 , 0.0669, 0.0292, 0.1107, 0.0357, 0.0402, 0.1553],
  192. # [ 0.0349, 0.1944, 0.0846, 0.0641, 0.0468, 0.0275, 0.4069, 0.0507, 0.0507, 0.0395],
  193.  
  194. subm = do_clip(preds,0.93) # subm - same as 'preds' above
  195.  
  196. subm_name = path+'results/subm.gz'
  197.  
  198. batches = get_batches(path+'train', batch_size=batch_size)
  199. classes = sorted(batches.class_indices, key=batches.class_indices.get)
  200.  
  201. submission = pd.DataFrame(subm, columns=classes)
  202. submission.insert(0, 'img', [a[4:] for a in test_filenames])
  203. submission.head()
  204.  
  205. submission.to_csv(subm_name, index=False, compression='gzip')
  206.  
  207. # FileLink(subm_name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement