Advertisement
Bananaware

shallow.py

Jun 5th, 2020
1,023
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.29 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import time, os, fnmatch, cv2
  3. import numpy as np
  4.  
  5. from keras.models import Sequential, load_model
  6. from keras.optimizers import SGD
  7. from keras.layers import Input, Dense, Conv2D, MaxPooling2D, AveragePooling2D, ZeroPadding2D, Dropout, Flatten, merge, Reshape, Activation
  8.  
  9. from sklearn.metrics import log_loss
  10.  
  11. import keras
  12. from keras import backend as K
  13. from sklearn.model_selection import StratifiedKFold
  14. from keras.utils import np_utils
  15. from keras import regularizers
  16. from sklearn.model_selection import train_test_split
  17. from sklearn.neighbors import KNeighborsClassifier as KNN
  18. from sklearn.metrics import accuracy_score, classification_report
  19.  
  20. from keras.layers import Activation, Dense ##
  21. from keras.layers.advanced_activations import LeakyReLU
  22.  
  23. image_size = 128
  24. image_size_squared = image_size**2
  25. num_classes = 2
  26.  
  27. training = False
  28. testing = True
  29.  
  30. num_splits = 10
  31.  
  32.  
  33. # consts train
  34. batch_size = 256
  35. nb_epoch = 10
  36. test_p = 0.20 # deprecated
  37. finetuned_path = 'custom_models/bois_newShallow-F65.h5'
  38. finetuned_path_save = 'custom_models/bois_newShallow-F65.h5'
  39. K.set_image_data_format('channels_last') # added
  40.  
  41.  
  42.  
  43.  
  44. # https://medium.com/tebs-lab/how-to-classify-mnist-digits-with-different-neural-network-architectures-39c75a0f03e3
  45.  
  46.  
  47. # aux functions
  48. def image_process(img_path, img_size, process=None):
  49.     img = cv2.imread(img_path, cv2.IMREAD_COLOR)
  50.     img = cv2.resize(img, (img_size, img_size))
  51.  
  52.     if process == None:
  53.         return np.float32(img)/255.0
  54.  
  55.     if 'contrast' in process:
  56.         img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
  57.         img[:,:,2] = cv2.equalizeHist(img[:,:,2])
  58.         img = cv2.cvtColor(img, cv2.COLOR_HSV2BGR)
  59.  
  60.     if 'negative' in process:
  61.         img = 255-img
  62.    
  63.     if 'gray' in process:
  64.         img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  65.  
  66.     if 'blur' in process:
  67.         img = cv2.GaussianBlur(img, (0, 0), 1.5)
  68.  
  69.     return np.float32(img)/255.0
  70.  
  71.  
  72. def load_data(data_folder='_test/qualidade-redo/', img_size=image_size, process='contrast_gray', show_time=True):
  73.     X = []
  74.     y = []
  75.     t0 = time.time()
  76.     n = 0
  77.  
  78.     for root, dirnames, filenames in os.walk(data_folder):
  79.         print(root)
  80.         for img in fnmatch.filter(filenames, '*.[Jj][Pp][Gg]'): # case insensitive
  81.             img = os.path.realpath(os.path.join(root,img))
  82.             lbl = img.split('/')[-2].strip() # label. mudar se estrutura dirs for diferente
  83.             X.append(image_process(img, img_size, process))
  84.             y.append(lbl)
  85.             n += 1
  86.  
  87.     if show_time:
  88.         t = time.time()-t0
  89.         print('Time to load "%s": %.2f seconds'%(data_folder, t))
  90.         print('\tMean time per image: %.4f seconds'%(t/n))
  91.     return np.array(X), np.array(y)
  92.  
  93.  
  94. def shallow_model():
  95.     model = Sequential()
  96.     model.add(Dense(32, kernel_regularizer=regularizers.l2(0.001), input_dim=image_size_squared, activation='relu'))
  97.     model.add(Dense(16, kernel_regularizer=regularizers.l2(0.001), activation='relu'))
  98.     model.add(Dense(2, activation='softmax'))
  99.     model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
  100.     return model
  101.  
  102.  
  103. def classify(X, y, X_test, y_test, classifier, show_time=False):
  104.     t0 = time.time()
  105.     clf = KNN(n_neighbors=1, n_jobs=-1) if classifier=='knn' else SVC(decision_function_shape='ovr')
  106.     clf.fit(X, y)
  107.     t1 = time.time()
  108.     pred = clf.predict(X_test)
  109.     if show_time:
  110.         t2 = time.time()
  111.         print('Classification time: %.4f seconds'%(t1-t0))
  112.         with open('logs/cnn_results.log', 'a+') as f:
  113.             f.write('\nFit time: '+str(t1-t0))
  114.             f.write('\nClassification time: '+str(t2-t1))
  115.     else:
  116.         return pred
  117.     acc = accuracy_score(y_test, pred)
  118.     print('\tAccuracy: ', acc)
  119.     return acc
  120.  
  121.  
  122. def load_append(vecX, vecY, data_folder):
  123.     X_temp, Y_temp = load_data(data_folder)
  124.     vecX = np.concatenate((vecX, X_temp))
  125.     vecY = np.concatenate((vecY, Y_temp))
  126.     return vecX, vecY
  127.  
  128.  
  129. '''
  130. A~ train: Puruna3, 1/2 USP (animais 1-27)
  131.   valid: Puruna1, 1/2 USP (animais 28-51)
  132.    test: Puruna2, Jersey
  133.   A-30 0.54   A-35 0.55   A-65 0.54.   valid 0.90+, A-30 0.93 (nao conseguiu representar Jersey?)
  134. '''
  135.  
  136. '''
  137. B~ train: Puruna3, Jersey
  138.   valid: Puruna2, 1/2 USP
  139.    test: Puruna1, 1/2 USP
  140.   B-30 0.80   B-50 0.75 (overfit).  valid 0.85
  141. '''
  142.  
  143. '''
  144. C~ train: Puruna3, 1/2 Jersey, 1/2 USP
  145.   valid: Puruna2, 1/2 Jersey
  146.    test: Puruna1, 1/2 USP
  147.   C-30 0.86   C-60 0.85   C-90 0.83
  148. '''
  149.  
  150. '''
  151. D~ train: Puruna3, Jersey3, USP3
  152.   valid: Puruna2, Jersey2, USP2
  153.    test: Puruna1, Jersey1, USP1
  154.   D-30 0.76   D-45 0.75   D-60 0.76
  155. '''
  156.  
  157. '''
  158. E~ train: Puruna3, Jersey2, Jersey3, USP2, USP3
  159.   valid: Puruna2, Jersey1
  160.    test: Puruna1, USP1
  161.   E-15 0.84  E-20 0.84  E-25 0.84  E-35 0.83  E-45 0.83   E-55 0.82   E-69 0.81
  162. '''
  163.  
  164. '''
  165. F~ train: Puruna3, Puruna2, Jersey2, USP2
  166.   valid: Puruna1-1, Jersey3, USP3
  167.    test: Puruna1-2, Jersey2, USP2
  168.   F-15 0.88  F-30 0.89  F-45 0.91  F-55 0.91  F-65 0.91
  169. '''
  170.  
  171.  
  172. if __name__ == '__main__':
  173.     if training == True:
  174.         X_train, Y_train = load_data(data_folder='_test/qualidade-newsample/Puruna3')
  175.         X_train, Y_train = load_append(X_train, Y_train, data_folder='_test/qualidade-newsample/Puruna2')
  176.         X_train, Y_train = load_append(X_train, Y_train, data_folder='_test/qualidade-newsample/Jersey2')
  177.         X_train, Y_train = load_append(X_train, Y_train, data_folder='_test/qualidade-newsample/USP2')
  178.        
  179.         X_valid, Y_valid = load_data(data_folder='_test/qualidade-newsample/Puruna1-1')
  180.         X_valid, Y_valid = load_append(X_valid, Y_valid, '_test/qualidade-newsample/Jersey3')
  181.         X_valid, Y_valid = load_append(X_valid, Y_valid, '_test/qualidade-newsample/USP3')
  182.        
  183.         X_train = X_train.reshape(X_train.shape[0], image_size_squared)
  184.         X_valid = X_valid.reshape(X_valid.shape[0], image_size_squared)
  185.        
  186.         # keras compatible format
  187.         Y_train = keras.utils.to_categorical(Y_train, num_classes)
  188.         Y_valid = keras.utils.to_categorical(Y_valid, num_classes)
  189.        
  190.         # faz fine-tuning se modelo fine-tuned nao existe
  191.         if (not (os.path.exists(finetuned_path))):
  192.             # Load our model
  193.             model = shallow_model()
  194.         else:
  195.             model = load_model(finetuned_path)
  196.         # Start Fine-tuning
  197.         model.fit(X_train, Y_train,
  198.                 batch_size=batch_size,
  199.                 nb_epoch=nb_epoch,
  200.                 shuffle=True,
  201.                 verbose=1,
  202.                 validation_data=(X_valid, Y_valid),
  203.                 )
  204.         model.save(finetuned_path_save)
  205.  
  206.     if testing == True: # testa o modelo com num_splits stratified k-fold
  207.         sumtotal = 0
  208.         sumcount = 0
  209.        
  210.         model = load_model(finetuned_path)
  211.         skf = StratifiedKFold(num_splits)
  212.         X_test_OG, Y_test_OG = load_data(data_folder='_test/qualidade-newsample/Puruna1-2')
  213.         X_test_OG, Y_test_OG = load_append(X_test_OG, Y_test_OG, data_folder='_test/qualidade-newsample/Jersey2')
  214.         X_test_OG, Y_test_OG = load_append(X_test_OG, Y_test_OG, data_folder='_test/qualidade-newsample/USP2')
  215.    
  216.         for train_index, test_index in skf.split(X_test_OG, Y_test_OG): # train, test
  217.             X_test = X_test_OG.reshape(X_test_OG.shape[0], image_size_squared)
  218.             Y_test = keras.utils.to_categorical(Y_test_OG, num_classes)
  219.             X, X_t = X_test[train_index], X_test[test_index]
  220.             y, y_t = Y_test[train_index], Y_test[test_index]
  221.  
  222.             print('Train Feature Extraction...')
  223.             t0 = time.time()
  224.             X = model.predict(x=X, batch_size=1)
  225.             t1 = time.time()
  226.             print('Train feature extraction finished in %f seconds.'%(t1-t0))
  227.             X_t = model.predict(x=X_t, batch_size=1)
  228.             t2 = time.time()
  229.             print('Test feature extraction finished in %f seconds.'%(t2-t1))
  230.             y_pred = classify(X, y, X_t, y_t, 'knn', False)
  231.             accscore = accuracy_score(y_t, y_pred)
  232.             sumtotal += len(y_t)
  233.             ntested = round(len(y_t)*accscore)
  234.             sumcount += ntested
  235.             print(classification_report(y_t, y_pred))
  236.             print('Acc:', accscore, len(y_t), len(y_t)*accscore)
  237.  
  238.         print('End:', sumcount, sumtotal, sumcount/sumtotal)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement