Advertisement
Mboshy

WNOkerasalak

Mar 19th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.85 KB | None | 0 0
  1. from keras.models import Sequential
  2. from keras.layers import Conv2D
  3. from keras.layers import Flatten, MaxPooling2D
  4. from keras.models import Sequential
  5. from keras.layers import Dense, Activation, Dropout
  6. from keras.models import load_model
  7. import numpy as np
  8.  
  9. import random
  10. import cv2
  11. import imutils
  12. import glob
  13.  
  14. def dawaj():
  15.     y_train = []
  16.     y_validate = []
  17.     size = (200, 200)
  18.     train_range = 200
  19.     random.seed()
  20.     for i in range(100):
  21.         a = random.randrange(1, 11)
  22.         b = random.randrange(0, 9)
  23.         image = cv2.imread('im{}.jpg'.format(a))
  24.         sword = cv2.imread('mieczalak/miecz{}.jpg'.format(b))
  25.         alfa = random.randrange(0, 360)
  26.         sword = imutils.rotate_bound(sword, alfa)
  27.         size2 = (int(np.shape(sword)[1]/2), int(np.shape(sword)[0]/2))
  28.         x = random.randrange(0, size[1] - size2[1])
  29.         y = random.randrange(0, size[0] - size2[0])
  30.         image = cv2.resize(image, size)
  31.         sword = cv2.resize(sword, size2)
  32.         for j in range(size2[0]):
  33.             for k in range(size2[1]):
  34.                 if sword[k, j, 0] == 0 and sword[k, j, 1] == 0 and sword[k, j, 2] == 0:
  35.                     pass
  36.                 else:
  37.                     image[x + k, y + j, 0] = sword[k, j, 0]
  38.                     image[x + k, y + j, 1] = sword[k, j, 1]
  39.                     image[x + k, y + j, 2] = sword[k, j, 2]
  40.         if i < 80:
  41.             cv2.imwrite('dataset/train/im{}.jpg'.format(i), image)
  42.             y_train.append([(y + size2[0]/2)/size[0], (x + size2[1]/2)/size[1]])
  43.         else:
  44.             cv2.imwrite('dataset/validate/im{}.jpg'.format(i), image)
  45.             y_validate.append([(y + size2[0]/2)/size[0], (x+size2[1]/2)/size[1]])
  46.  
  47.     x_train = [cv2.imread(file) for file in glob.glob('./dataset/train/*.jpg')]
  48.     x_validate = [cv2.imread(file) for file in glob.glob('./dataset/validate/*.jpg')]
  49.     x_train = np.array(x_train)
  50.     x_validate = np.array(x_validate)
  51.     x_train = (x_train)/255
  52.     x_validate = (x_validate)/255
  53.     y_train = np.array(y_train)
  54.     y_validate = np.array(y_validate)
  55.     return x_train, y_train, x_validate, y_validate, size
  56.  
  57.  
  58. x_train, y_train, x_validate, y_validate, size = dawaj()
  59.  
  60. classifier = Sequential([
  61.         Conv2D(32, (5, 5), input_shape=(size[0], size[1], 3), activation='relu'),
  62.         MaxPooling2D(pool_size=(2, 2)),
  63.         Conv2D(32, (5, 5), activation='sigmoid'),
  64.         MaxPooling2D(pool_size=(2, 2)),
  65.         Flatten(),
  66.         Dense(units=128, activation='sigmoid'),
  67.         Dropout(rate=0.4),
  68.         Dense(units=2, activation='sigmoid')
  69. ])
  70.  
  71. classifier.compile('adadelta', 'mse', metrics=['accuracy'])
  72. # classifier.compile('adam', 'mse', metrics=['accuracy'])
  73. #classifier = load_model('model_lab3.h5')
  74. classifier.fit(x_train, y_train, epochs=40, validation_data=(x_validate, y_validate))
  75.  
  76. classifier.save('model_lab3.h5')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement