Advertisement
akashtadwai

Untitled

Dec 28th, 2019
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.54 KB | None | 0 0
  1. %tensorflow_version 2.x
  2. import tensorflow as tf
  3. print(tf.__version__)
  4.  
  5.  
  6. from tensorflow.keras.applications import ResNet50
  7. from tensorflow.keras.models import Sequential
  8. from tensorflow.keras.layers import Dense, InputLayer, Flatten, GlobalAveragePooling2D
  9. num_classes = 2
  10. IMG_SIZE = 224
  11. IMG_SHAPE = (IMG_SIZE, IMG_SIZE, 3)
  12. my_new_model=tf.keras.applications.ResNet50(include_top=False, weights='imagenet', input_shape=IMG_SHAPE, pooling='avg', classes=2)
  13. my_new_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
  14.  
  15.  
  16. !wget --no-check-certificate \
  17.    https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip -O \
  18.    /tmp/cats_and_dogs_filtered.zip
  19.  
  20.  
  21. import os
  22. import zipfile
  23.  
  24. local_zip = '/tmp/cats_and_dogs_filtered.zip'
  25. zip_ref = zipfile.ZipFile(local_zip, 'r')
  26. zip_ref.extractall('/tmp')
  27. zip_ref.close()
  28.  
  29. base_dir = '/tmp/cats_and_dogs_filtered'
  30. train_dir = os.path.join(base_dir, 'train')
  31. validation_dir = os.path.join(base_dir, 'validation')
  32.  
  33. # Directory with our training cat pictures
  34. train_cats_dir = os.path.join(train_dir, 'cats')
  35.  
  36. # Directory with our training dog pictures
  37. train_dogs_dir = os.path.join(train_dir, 'dogs')
  38.  
  39. # Directory with our validation cat pictures
  40. validation_cats_dir = os.path.join(validation_dir, 'cats')
  41.  
  42. # Directory with our validation dog pictures
  43. validation_dogs_dir = os.path.join(validation_dir, 'dogs')
  44.  
  45. train_cat_fnames = os.listdir(train_cats_dir)
  46. train_dog_fnames = os.listdir(train_dogs_dir)
  47.  
  48.  
  49. from tensorflow.keras.preprocessing.image import ImageDataGenerator
  50. from tensorflow.keras.applications.resnet50 import preprocess_input
  51. train_datagen = ImageDataGenerator(
  52.     preprocessing_function=preprocess_input,
  53.     rotation_range=40,
  54.     width_shift_range=0.2,
  55.     height_shift_range=0.2,
  56.     shear_range=0.2,
  57.     zoom_range=0.2,
  58.     horizontal_flip=True,)
  59.  
  60. # Note that the validation data should not be augmented!
  61. test_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)
  62.  
  63. train_generator = train_datagen.flow_from_directory(
  64.         train_dir,  # This is the source directory for training images
  65.         target_size=(224,224),  # All images will be resized to 224x224
  66.         batch_size=20,
  67.         class_mode='binary')
  68.  
  69. validation_generator = test_datagen.flow_from_directory(
  70.         validation_dir,
  71.         target_size=(224, 224),
  72.         class_mode='binary')
  73.  
  74. my_new_model.fit_generator(
  75.         train_generator,
  76.         epochs = 8,
  77.         steps_per_epoch=100,
  78.         validation_data=validation_generator)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement