|
| 1 | +# CIFAR - 10 |
| 2 | + |
| 3 | +# To decode the files |
| 4 | +import pickle |
| 5 | +# For array manipulations |
| 6 | +import numpy as np |
| 7 | +# To make one-hot vectors |
| 8 | +from keras.utils import np_utils |
| 9 | +# To plot graphs and display images |
| 10 | +from matplotlib import pyplot as plt |
| 11 | + |
| 12 | + |
| 13 | +#constants |
| 14 | + |
| 15 | +path = "data/" # Path to data |
| 16 | + |
| 17 | +# Height or width of the images (32 x 32) |
| 18 | +size = 32 |
| 19 | + |
| 20 | +# 3 channels: Red, Green, Blue (RGB) |
| 21 | +channels = 3 |
| 22 | + |
| 23 | +# Number of classes |
| 24 | +num_classes = 10 |
| 25 | + |
| 26 | +# Each file contains 10000 images |
| 27 | +image_batch = 10000 |
| 28 | + |
| 29 | +# 5 training files |
| 30 | +num_files_train = 5 |
| 31 | + |
| 32 | +# Total number of training images |
| 33 | +images_train = image_batch * num_files_train |
| 34 | + |
| 35 | +# https://www.cs.toronto.edu/~kriz/cifar.html |
| 36 | + |
| 37 | + |
| 38 | +def unpickle(file): |
| 39 | + |
| 40 | + # Convert byte stream to object |
| 41 | + with open(path + file,'rb') as fo: |
| 42 | + print("Decoding file: %s" % (path+file)) |
| 43 | + dict = pickle.load(fo, encoding='bytes') |
| 44 | + |
| 45 | + # Dictionary with images and labels |
| 46 | + return dict |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +def convert_images(raw_images): |
| 52 | + |
| 53 | + # Convert images to numpy arrays |
| 54 | + |
| 55 | + # Convert raw images to numpy array and normalize it |
| 56 | + raw = np.array(raw_images, dtype = float) / 255.0 |
| 57 | + |
| 58 | + # Reshape to 4-dimensions - [image_number, channel, height, width] |
| 59 | + images = raw.reshape([-1, channels, size, size]) |
| 60 | + |
| 61 | + images = images.transpose([0, 2, 3, 1]) |
| 62 | + |
| 63 | + # 4D array - [image_number, height, width, channel] |
| 64 | + return images |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +def load_data(file): |
| 70 | + # Load file, unpickle it and return images with their labels |
| 71 | + |
| 72 | + data = unpickle(file) |
| 73 | + |
| 74 | + # Get raw images |
| 75 | + images_array = data[b'data'] |
| 76 | + |
| 77 | + # Convert image |
| 78 | + images = convert_images(images_array) |
| 79 | + # Convert class number to numpy array |
| 80 | + labels = np.array(data[b'labels']) |
| 81 | + |
| 82 | + # Images and labels in np array form |
| 83 | + return images, labels |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | +def get_test_data(): |
| 89 | + # Load all test data |
| 90 | + |
| 91 | + images, labels = load_data(file = "test_batch") |
| 92 | + |
| 93 | + # Images, their labels and |
| 94 | + # corresponding one-hot vectors in form of np arrays |
| 95 | + return images, labels, np_utils.to_categorical(labels,num_classes) |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | +def get_train_data(): |
| 101 | + # Load all training data in 5 files |
| 102 | + |
| 103 | + # Pre-allocate arrays |
| 104 | + images = np.zeros(shape = [images_train, size, size, channels], dtype = float) |
| 105 | + labels = np.zeros(shape=[images_train],dtype = int) |
| 106 | + |
| 107 | + # Starting index of training dataset |
| 108 | + start = 0 |
| 109 | + |
| 110 | + # For all 5 files |
| 111 | + for i in range(num_files_train): |
| 112 | + |
| 113 | + # Load images and labels |
| 114 | + images_batch, labels_batch = load_data(file = "data_batch_" + str(i+1)) |
| 115 | + |
| 116 | + # Calculate end index for current batch |
| 117 | + end = start + image_batch |
| 118 | + |
| 119 | + # Store data to corresponding arrays |
| 120 | + images[start:end,:] = images_batch |
| 121 | + labels[start:end] = labels_batch |
| 122 | + |
| 123 | + # Update starting index of next batch |
| 124 | + start = end |
| 125 | + |
| 126 | + # Images, their labels and |
| 127 | + # corresponding one-hot vectors in form of np arrays |
| 128 | + return images, labels, np_utils.to_categorical(labels,num_classes) |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | +def get_class_names(): |
| 133 | + |
| 134 | + # Load class names |
| 135 | + raw = unpickle("batches.meta")[b'label_names'] |
| 136 | + |
| 137 | + # Convert from binary strings |
| 138 | + names = [x.decode('utf-8') for x in raw] |
| 139 | + |
| 140 | + # Class names |
| 141 | + return names |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | +def plot_images(images, labels_true, class_names, labels_pred=None): |
| 146 | + |
| 147 | + assert len(images) == len(labels_true) |
| 148 | + |
| 149 | + # Create a figure with sub-plots |
| 150 | + fig, axes = plt.subplots(3, 3, figsize = (8,8)) |
| 151 | + |
| 152 | + # Adjust the vertical spacing |
| 153 | + if labels_pred is None: |
| 154 | + hspace = 0.2 |
| 155 | + else: |
| 156 | + hspace = 0.5 |
| 157 | + fig.subplots_adjust(hspace=hspace, wspace=0.3) |
| 158 | + |
| 159 | + for i, ax in enumerate(axes.flat): |
| 160 | + # Fix crash when less than 9 images |
| 161 | + if i < len(images): |
| 162 | + # Plot the image |
| 163 | + ax.imshow(images[i], interpolation='spline16') |
| 164 | + |
| 165 | + # Name of the true class |
| 166 | + labels_true_name = class_names[labels_true[i]] |
| 167 | + |
| 168 | + # Show true and predicted classes |
| 169 | + if labels_pred is None: |
| 170 | + xlabel = "True: "+labels_true_name |
| 171 | + else: |
| 172 | + # Name of the predicted class |
| 173 | + labels_pred_name = class_names[labels_pred[i]] |
| 174 | + |
| 175 | + xlabel = "True: "+labels_true_name+"\nPredicted: "+ labels_pred_name |
| 176 | + |
| 177 | + # Show the class on the x-axis |
| 178 | + ax.set_xlabel(xlabel) |
| 179 | + |
| 180 | + # Remove ticks from the plot |
| 181 | + ax.set_xticks([]) |
| 182 | + ax.set_yticks([]) |
| 183 | + |
| 184 | + # Show the plot |
| 185 | + plt.show() |
| 186 | + |
| 187 | + |
| 188 | +def plot_model(model_details): |
| 189 | + |
| 190 | + # Create sub-plots |
| 191 | + fig, axs = plt.subplots(1,2,figsize=(15,5)) |
| 192 | + |
| 193 | + # Summarize history for accuracy |
| 194 | + axs[0].plot(range(1,len(model_details.history['acc'])+1),model_details.history['acc']) |
| 195 | + axs[0].plot(range(1,len(model_details.history['val_acc'])+1),model_details.history['val_acc']) |
| 196 | + axs[0].set_title('Model Accuracy') |
| 197 | + axs[0].set_ylabel('Accuracy') |
| 198 | + axs[0].set_xlabel('Epoch') |
| 199 | + axs[0].set_xticks(np.arange(1,len(model_details.history['acc'])+1),len(model_details.history['acc'])/10) |
| 200 | + axs[0].legend(['train', 'val'], loc='best') |
| 201 | + |
| 202 | + # Summarize history for loss |
| 203 | + axs[1].plot(range(1,len(model_details.history['loss'])+1),model_details.history['loss']) |
| 204 | + axs[1].plot(range(1,len(model_details.history['val_loss'])+1),model_details.history['val_loss']) |
| 205 | + axs[1].set_title('Model Loss') |
| 206 | + axs[1].set_ylabel('Loss') |
| 207 | + axs[1].set_xlabel('Epoch') |
| 208 | + axs[1].set_xticks(np.arange(1,len(model_details.history['loss'])+1),len(model_details.history['loss'])/10) |
| 209 | + axs[1].legend(['train', 'val'], loc='best') |
| 210 | + |
| 211 | + # Show the plot |
| 212 | + plt.show() |
| 213 | + |
| 214 | + |
| 215 | + |
| 216 | +def visualize_errors(images_test, labels_test, class_names, labels_pred, correct): |
| 217 | + |
| 218 | + incorrect = (correct == False) |
| 219 | + |
| 220 | + # Images of the test-set that have been incorrectly classified. |
| 221 | + images_error = images_test[incorrect] |
| 222 | + |
| 223 | + # Get predicted classes for those images |
| 224 | + labels_error = labels_pred[incorrect] |
| 225 | + |
| 226 | + # Get true classes for those images |
| 227 | + labels_true = labels_test[incorrect] |
| 228 | + |
| 229 | + |
| 230 | + # Plot the first 9 images. |
| 231 | + plot_images(images=images_error[0:9], |
| 232 | + labels_true=labels_true[0:9], |
| 233 | + class_names=class_names, |
| 234 | + labels_pred=labels_error[0:9]) |
| 235 | + |
| 236 | + |
| 237 | +def predict_classes(model, images_test, labels_test): |
| 238 | + |
| 239 | + # Predict class of image using model |
| 240 | + class_pred = model.predict(images_test, batch_size=32) |
| 241 | + |
| 242 | + # Convert vector to a label |
| 243 | + labels_pred = np.argmax(class_pred,axis=1) |
| 244 | + |
| 245 | + # Boolean array that tell if predicted label is the true label |
| 246 | + correct = (labels_pred == labels_test) |
| 247 | + |
| 248 | + # Array which tells if the prediction is correct or not |
| 249 | + # And predicted labels |
| 250 | + return correct, labels_pred |
| 251 | + |
| 252 | + |
0 commit comments