Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.24 KB | None | 0 0
  1. # from keras.datasets import cifar10 # subroutines for fetching the CIFAR-10 dataset
  2.  
  3. import numpy as np
  4. from PreparedData import *
  5.  
  6.  
  7. def Test(sample_indexes):
  8.     sample_images = np.array([X_train[i] for i in sample_indexes])
  9.     sample_labels = [y_train[i] for i in sample_indexes]
  10.  
  11.     out1 = model.predict(sample_images)
  12.     print()
  13.     print(sample_labels)
  14.  
  15.     print([np.argmax(i) for i in out1])
  16.     print(out1)
  17.  
  18.  
  19. batch_size = 52 # in each iteration, we consider 32 training examples at once
  20. num_epochs = 24 # we iterate 200 times over the entire training set
  21. kernel_size = 3 # we will use 3x3 kernels throughout
  22. pool_size = 2 # we will use 2x2 pooling throughout
  23. conv_depth_1 = 32 # we will initially have 32 kernels per conv. layer...
  24. conv_depth_2 = 64 # ...switching to 64 after the first pooling layer
  25. drop_prob_1 = 0.25 # dropout after pooling with probability 0.25
  26. drop_prob_2 = 0.51 # dropout in the FC layer with probability 0.5
  27. hidden_size = 512 # the FC layer will have 512 neurons
  28.  
  29.  
  30.  
  31.  
  32. dataTrain = filePrepared(r'C:\Users\tutor\PycharmProjects\exelePreparations\scrapTRAIn')
  33. dataTrain.generateLabelsName()
  34. dataTrain.simpleLoadData()
  35. # data.createTestData()
  36. dataTrain.convertToArray()
  37.  
  38. dataTest = filePrepared(r'C:\Users\tutor\PycharmProjects\exelePreparations\scrapTEST')
  39. dataTest.generateLabelsName()
  40.  
  41. dataTest.simpleLoadData()
  42.  
  43.  
  44. dataTest.convertToArray()
  45. print(dataTest.getShape()[0])
  46. # print(data.getSize())
  47.  
  48. (X_train, y_train), (X_test, y_test) = (dataTrain.images,dataTrain.labels),(dataTest.images,dataTest.labels) # fetch CIFAR-10 data
  49.  
  50. num_train, height, width,depth  = dataTrain.getShape() # there are 50000 training examples in CIFAR-10
  51.  
  52. num_test = dataTest.getShape()[0] # there are 10000 test examples in CIFAR-10
  53. num_classes = dataTrain.sizeLabels() # there are 10 image classes
  54. print(num_classes)
  55. X_train = X_train.astype('float32')
  56. X_test = X_test.astype('float32')
  57. X_train /= np.max(X_train) # Normalise data to [0, 1] range
  58. X_test /= np.max(X_test) # Normalise data to [0, 1] range
  59.  
  60. from keras.models import Model # basic class for specifying and training a neural network
  61. from keras.layers import Input, Convolution2D, MaxPooling2D, Dense, Dropout, Flatten
  62. from keras.utils import np_utils # utilities for one-hot encoding of ground truth values
  63.  
  64. Y_train = np_utils.to_categorical(y_train, num_classes) # One-hot encode the labels
  65. Y_test = np_utils.to_categorical(y_test, num_classes) # One-hot encode the labels
  66.  
  67.  
  68.  
  69.  
  70. inp = Input(shape=(height, width,depth)) # N.B. depth goes first in Keras!
  71. # Conv [32] -> Conv [32] -> Pool (with dropout on the pooling layer)
  72. conv_1 = Convolution2D(conv_depth_1, kernel_size, kernel_size, border_mode='same', activation='relu')(inp)
  73. conv_2 = Convolution2D(conv_depth_1, kernel_size, kernel_size, border_mode='same', activation='relu')(conv_1)
  74. pool_1 = MaxPooling2D(pool_size=(pool_size, pool_size))(conv_2)
  75. drop_1 = Dropout(drop_prob_1)(pool_1)
  76. # Conv [64] -> Conv [64] -> Pool (with dropout on the pooling layer)
  77. conv_3 = Convolution2D(conv_depth_2, kernel_size, kernel_size, border_mode='same', activation='relu')(drop_1)
  78. # conv_4 = Convolution2D(conv_depth_2, kernel_size, kernel_size, border_mode='same', activation='relu')(conv_3)
  79.  
  80. pool_2 = MaxPooling2D(pool_size=(pool_size, pool_size))(conv_3)
  81. drop_2 = Dropout(drop_prob_1)(pool_2)
  82. # Now flatten to 1D, apply FC -> ReLU (with dropout) -> softmax
  83. flat = Flatten()(drop_2)
  84. hidden = Dense(hidden_size, activation='relu')(flat)
  85. drop_3 = Dropout(drop_prob_2)(hidden)
  86. out = Dense(num_classes, activation='softmax')(drop_3)
  87.  
  88. model = Model(input=inp, output=out) # To define a model, just specify its input and output layers
  89.  
  90. model.compile(loss='categorical_crossentropy', # using the cross-entropy loss function
  91.               optimizer='adam', # using the Adam optimiser
  92.               metrics=['accuracy']) # reporting the accuracy
  93.  
  94. model.fit(X_train, Y_train, # Train the model using the training set...
  95.           batch_size=batch_size, nb_epoch=num_epochs,
  96.           verbose=1, validation_split=0.15,shuffle=True) # ...holding out 10% of the data for validation
  97.  
  98. source = model.evaluate(X_test, Y_test, verbose=1) # Evaluate the trained model on the test set!
  99. print(source)
  100. Test([4,6,3,57,3,234,7,345,2,45,786,435,234,100])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement