Guest User

Untitled

a guest
Nov 14th, 2019
144
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import cv2
  2. import keras
  3. import numpy as np
  4. from keras import backend as K, Sequential
  5. from keras.layers import Conv2D, Flatten, Dense, Dropout, Activation, GlobalAveragePooling2D
  6. from keras.optimizers import Adam
  7. from keras.preprocessing.image import ImageDataGenerator
  8. from matplotlib import pyplot as plt
  9. # %matplotlib inline
  10.  
  11. TRAIN_DIR = './images/train'
  12. TEST_DIR = './images/test'
  13. VALID_DIR = './images/valid'
  14. IMAGE_CLASS = ['Anggur', 'Apel', 'ApelH', 'Jeruk', 'Kiwi', 'Lemon', 'Mangga', 'Melon', 'Naga', 'Pisang', 'Salak',
  15.                'Sirsak']
  16.  
  17. TRAIN_BATCH = ImageDataGenerator().flow_from_directory(TRAIN_DIR, target_size=(224, 224), classes=IMAGE_CLASS,
  18.                                                        batch_size=10)
  19. TEST_BATCH = ImageDataGenerator().flow_from_directory(TEST_DIR, target_size=(224, 224), classes=IMAGE_CLASS,
  20.                                                       batch_size=10)
  21. VALID_BATCH = ImageDataGenerator().flow_from_directory(VALID_DIR, target_size=(224, 224), classes=IMAGE_CLASS,
  22.                                                        batch_size=10)
  23.  
  24. vgg16_model = keras.applications.vgg16.VGG16()
  25.  
  26. model = Sequential()
  27. for layer in vgg16_model.layers:
  28.     model.add(layer)
  29.  
  30. model.layers.pop()
  31.  
  32. for layer in model.layers:
  33.     layer.trainable = False
  34.  
  35. model.add(Dense(12, activation='softmax'))
  36. # model.summary()
  37. model.compile(Adam(lr=.0001), loss='categorical_crossentropy', metrics=['accuracy'])
  38. model.fit_generator(TRAIN_BATCH, steps_per_epoch=15, validation_data=VALID_BATCH, validation_steps=15, epochs=100, verbose=1)
RAW Paste Data