# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + id="9I13EoLdvSkL" executionInfo={"status": "ok", "timestamp": 1616593757196, "user_tz": -480, "elapsed": 2915, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/-BV-vQrnIvTY/AAAAAAAAAAI/AAAAAAAADxw/wSzRzhF8b6c/s64/photo.jpg", "userId": "01517089400226719298"}}
import os, signal, zipfile, random
import numpy as np
from google.colab import files
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import tensorflow as tf
from keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing.image import img_to_array, load_img

# + id="baWB9qSLfxKc"


# + id="wL0eTtKQwXbc" colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1608473578407, "user_tz": -480, "elapsed": 3465, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/-BV-vQrnIvTY/AAAAAAAAAAI/AAAAAAAADxw/wSzRzhF8b6c/s64/photo.jpg", "userId": "01517089400226719298"}} outputId="af5ddb54-c562-4e87-ac0d-4afdd5174949"
# !wget --no-check-certificate \
#     https://storage.googleapis.com/laurencemoroney-blog.appspot.com/rps.zip \
#     -O /tmp/rps.zip
  
# !wget --no-check-certificate \
#     https://storage.googleapis.com/laurencemoroney-blog.appspot.com/rps-test-set.zip \
#     -O /tmp/rps-test-set.zip

# + id="XY_qBS1Uu50_"
local_zip = '/tmp/rps.zip'
zip_ref = zipfile.ZipFile(local_zip, 'r')
zip_ref.extractall('/tmp/')
zip_ref.close()

local_zip = '/tmp/rps-test-set.zip'
zip_ref = zipfile.ZipFile(local_zip, 'r')
zip_ref.extractall('/tmp/')
zip_ref.close()

# + id="-fm9PrOlwcLg"
train_dir = os.path.join('/tmp/rps')
train_rock_dir = os.path.join(train_dir, 'rock')
train_paper_dir = os.path.join(train_dir, 'paper')
train_scissors_dir = os.path.join(train_dir, 'scissors')

test_dir = os.path.join('/tmp/rps-test-set')
test_rock_dir = os.path.join(test_dir, 'rock')
test_paper_dir = os.path.join(test_dir, 'paper')
test_scissors_dir = os.path.join(test_dir, 'scissors')

# + id="AJ2SgEeixIfv" colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1608473580813, "user_tz": -480, "elapsed": 1044, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/-BV-vQrnIvTY/AAAAAAAAAAI/AAAAAAAADxw/wSzRzhF8b6c/s64/photo.jpg", "userId": "01517089400226719298"}} outputId="030964ef-8faf-4da3-835e-51b43c7678c8"
print('total training rock images:', len(os.listdir(train_rock_dir)))
print('total training paper images:', len(os.listdir(train_paper_dir)))
print('total training scissors images:', len(os.listdir(train_scissors_dir)))
print('total testing rock images:', len(os.listdir(test_rock_dir)))
print('total testing paper images:', len(os.listdir(test_paper_dir)))
print('total testing scissors images:', len(os.listdir(test_scissors_dir)))

rock_files = os.listdir(train_rock_dir)
print(rock_files[:10])

paper_files = os.listdir(train_paper_dir)
print(paper_files[:10])

scissors_files = os.listdir(train_scissors_dir)
print(scissors_files[:10])

# + id="C1P67KWltXYu"
nrows = 2
ncols = 3
pic_index = 0

# + id="yMKghSRcxYlK" colab={"base_uri": "https://localhost:8080/", "height": 357} executionInfo={"status": "ok", "timestamp": 1590830783955, "user_tz": -480, "elapsed": 8452, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/-BV-vQrnIvTY/AAAAAAAAAAI/AAAAAAAADxw/wSzRzhF8b6c/s64/photo.jpg", "userId": "01517089400226719298"}} outputId="3c7a21ef-c9fc-475c-f466-2bbd0213a7ac"
fig = plt.gcf()
fig.set_size_inches(ncols * 3, nrows * 3)

pic_index += 2
next_rock_pix = [os.path.join(train_rock_dir, fname) 
                for fname in rock_files[pic_index-2:pic_index]]
next_paper_pix = [os.path.join(train_paper_dir, fname) 
                for fname in paper_files[pic_index-2:pic_index]]
next_scissors_pix = [os.path.join(train_scissors_dir, fname) 
                for fname in scissors_files[pic_index-2:pic_index]]

for i, img_path in enumerate(next_rock_pix + next_paper_pix + next_scissors_pix):
  sp = plt.subplot(nrows, ncols, i + 1)
  sp.axis('Off')
  img = mpimg.imread(img_path)
  plt.imshow(img)

plt.show()

# + id="XFSyUV5M2qSZ" colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1608473585702, "user_tz": -480, "elapsed": 862, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/-BV-vQrnIvTY/AAAAAAAAAAI/AAAAAAAADxw/wSzRzhF8b6c/s64/photo.jpg", "userId": "01517089400226719298"}} outputId="fd1238a7-5efb-4482-a3d0-44c036a02ee5"
train_datagen = ImageDataGenerator(
      rescale = 1./255,
	    rotation_range=40,
      width_shift_range=0.2,
      height_shift_range=0.2,
      shear_range=0.2,
      zoom_range=0.2,
      brightness_range=[0.8,1.2],
      horizontal_flip=True,
      fill_mode='nearest'
)

train_generator = train_datagen.flow_from_directory(
    train_dir,
    target_size = (150, 150),
    batch_size = 126,
    class_mode = 'categorical'
)

validation_datagen = ImageDataGenerator(rescale=1./255)
validation_generator = train_datagen.flow_from_directory(
    test_dir,
    target_size = (150, 150),
    batch_size = 124,
    class_mode = 'categorical'
)

# + id="EXpibkMH14ee"
model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(64, (3,3), activation='relu', input_shape=(150, 150, 3)),
    tf.keras.layers.MaxPooling2D(2, 2),
    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dropout(0.5),
    tf.keras.layers.Dense(512, activation='relu'),
    tf.keras.layers.Dense(3, activation='softmax')
])

# + id="Lmyt-TIU2AwJ" colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1608473593821, "user_tz": -480, "elapsed": 944, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/-BV-vQrnIvTY/AAAAAAAAAAI/AAAAAAAADxw/wSzRzhF8b6c/s64/photo.jpg", "userId": "01517089400226719298"}} outputId="fb6fee8f-925b-4d5d-f188-6c504d280351"
model.summary()

# + id="Tbi_JRSFetow"
checkpoint_path = "/rps_model/cp-{epoch:04d}.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)

cp_callback = tf.keras.callbacks.ModelCheckpoint(
    filepath=checkpoint_path, 
    verbose=1, 
    save_weights_only=True
)

# + id="bHhwvFR72W1a"
model.compile(
    loss = 'categorical_crossentropy',
    optimizer = 'rmsprop',
    metrics = ['accuracy']
)

model.save_weights(checkpoint_path.format(epoch=0))

# + id="HUEvi_0x3oti" colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1608474410873, "user_tz": -480, "elapsed": 629891, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/-BV-vQrnIvTY/AAAAAAAAAAI/AAAAAAAADxw/wSzRzhF8b6c/s64/photo.jpg", "userId": "01517089400226719298"}} outputId="087b8ef1-6c87-4c3d-be98-2684792f04e9"
history = model.fit(
    train_generator,
    steps_per_epoch = 20,
    epochs = 25,
    callbacks=[cp_callback],
    validation_data = validation_generator,
    validation_steps = 3
)

# + id="nkzDJSi04d2p" colab={"base_uri": "https://localhost:8080/", "height": 562} executionInfo={"status": "ok", "timestamp": 1608475811782, "user_tz": -480, "elapsed": 1589, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/-BV-vQrnIvTY/AAAAAAAAAAI/AAAAAAAADxw/wSzRzhF8b6c/s64/photo.jpg", "userId": "01517089400226719298"}} outputId="bea81b64-8365-4835-d80f-28b3106726b9"
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']

epochs = range(len(acc))

plt.plot(epochs, acc, 'r', label='Training accuracy')
plt.plot(epochs, val_acc, 'b', label='Validation accuracy')
plt.title('Training and validation accuracy')
plt.legend(loc=0)
plt.figure()

plt.plot(epochs, loss, 'r', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')

# + id="v81fHx3g6Yfu" colab={"resources": {"http://localhost:8080/nbextensions/google.colab/files.js": {"data": "<KEY> "ok": true, "headers": [["content-type", "application/javascript"]], "status": 200, "status_text": ""}}, "base_uri": "https://localhost:8080/", "height": 898} executionInfo={"status": "ok", "timestamp": 1590588369657, "user_tz": -480, "elapsed": 69652, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/-BV-vQrnIvTY/AAAAAAAAAAI/AAAAAAAADxw/wSzRzhF8b6c/s64/photo.jpg", "userId": "01517089400226719298"}} outputId="547776de-6349-4c09-821d-ca31c3e5c137"
uploaded = files.upload()

for fn in uploaded.keys():
 
  path = '/content/' + fn
  img = image.load_img(path, target_size=(150, 150))
  x = image.img_to_array(img)
  x = np.expand_dims(x, axis=0)

  images = np.vstack([x])
  classes = model.predict(images, batch_size=10)
  print(fn)
  print(classes)

# + id="FSn7ejEJ-uVk" colab={"base_uri": "https://localhost:8080/", "height": 673} executionInfo={"status": "ok", "timestamp": 1590580608774, "user_tz": -480, "elapsed": 3875, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/-BV-vQrnIvTY/AAAAAAAAAAI/AAAAAAAADxw/wSzRzhF8b6c/s64/photo.jpg", "userId": "01517089400226719298"}} outputId="9c0bf7a7-e969-46fd-8918-e75f1460529f"
# Let's define a new Model that will take an image as input, and will output
# intermediate representations for all layers in the previous model after
# the first.
successive_outputs = [layer.output for layer in model.layers[1:]]
#visualization_model = Model(img_input, successive_outputs)
visualization_model = tf.keras.models.Model(inputs = model.input, outputs = successive_outputs)
# Let's prepare a random input image from the training set.
horse_img_files = [os.path.join(train_horse_dir, f) for f in train_horse_names]
human_img_files = [os.path.join(train_human_dir, f) for f in train_human_names]
img_path = random.choice(horse_img_files + human_img_files)

img = load_img(img_path, target_size=(300, 300))  # this is a PIL image
x = img_to_array(img)  # Numpy array with shape (150, 150, 3)
x = x.reshape((1,) + x.shape)  # Numpy array with shape (1, 150, 150, 3)

# Rescale by 1/255
x /= 255

# Let's run our image through our network, thus obtaining all
# intermediate representations for this image.
successive_feature_maps = visualization_model.predict(x)

# These are the names of the layers, so can have them as part of our plot
layer_names = [layer.name for layer in model.layers]

# Now let's display our representations
for layer_name, feature_map in zip(layer_names, successive_feature_maps):
  if len(feature_map.shape) == 4:
    # Just do this for the conv / maxpool layers, not the fully-connected layers
    n_features = feature_map.shape[-1]  # number of features in feature map
    # The feature map has shape (1, size, size, n_features)
    size = feature_map.shape[1]
    # We will tile our images in this matrix
    display_grid = np.zeros((size, size * n_features))
    for i in range(n_features):
      # Postprocess the feature to make it visually palatable
      x = feature_map[0, :, :, i]
      x -= x.mean()
      x /= x.std()
      x *= 64
      x += 128
      x = np.clip(x, 0, 255).astype('uint8')
      # We'll tile each filter into this big horizontal grid
      display_grid[:, i * size : (i + 1) * size] = x
    # Display the grid
    scale = 20. / n_features
    plt.figure(figsize=(scale * n_features, scale))
    plt.title(layer_name)
    plt.grid(False)
    plt.imshow(display_grid, aspect='auto', cmap='viridis')

StarcoderdataJupyterScriptsDedupFiltered