<jupyter_start><jupyter_text>Import depends<jupyter_code>import keras
from keras.callbacks import EarlyStopping
from keras.datasets import cifar10
from keras.layers import Activation, Conv2D, Dense, Dropout, Flatten, MaxPooling2D
from keras.models import Sequential
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import numpy as np
import random
from sklearn import preprocessing

import tensorflow as tf
from tensorflow.contrib import eager as tfe 
print('tf.__version__:', tf.__version__)
print('tf.keras.__version__:', tf.keras.__version__)
print('keras.__version__:', keras.__version__)<jupyter_output>tf.__version__: 1.9.0-rc0
tf.keras.__version__: 2.1.6-tf
keras.__version__: 2.2.0<jupyter_text>Simple model<jupyter_code>model = keras.Sequential()
# Adds a densely-connected layer with 64 units to the model:
model.add(keras.layers.Dense(64, activation='relu'))
# Add another:
model.add(keras.layers.Dense(64, activation='relu'))
# Add a softmax layer with 10 output units:
model.add(keras.layers.Dense(10, activation='softmax'))
print(model.to_yaml())
def my_py_func(x):
    x = tf.matmul(x, x)  # You can use tf ops
    print(x)  # but it's eager!
    return x

with tf.Session() as sess:
    x = tf.placeholder(dtype=tf.float32)
    # Call eager function in graph!
    #x = tfe.Variable([[2]])
    pf = tfe.py_func(my_py_func, [x], tf.float32)
    sess.run(pf, feed_dict={x: [[2.0]]})  # [[4.0]]<jupyter_output>tf.Tensor([[4.]], shape=(1, 1), dtype=float32)<jupyter_text>Save / Restore model  Prepare datasetmore about data set https://www.cs.toronto.edu/~kriz/cifar.html- related challange on kaggle https://www.kaggle.com/c/cifar-10/leaderboard- manual classification by <NAME> http://karpathy.github.io/2011/04/27/manually-classifying-cifar10/<jupyter_code># util functions
def grid_of_images(cols, rows, image_callback, grid_width=8):
    """
    Show images in a grid
    """
    plt.figure(figsize=(grid_width, grid_width / cols * rows))
    gs = gridspec.GridSpec(rows, cols)

    for row_idx in range(rows):
        for col_idx in range(cols):
            plt.subplot(gs[row_idx, col_idx])
            plt.axis('off')
            image, title = image_callback(row_idx, col_idx)
            plt.imshow(image, interpolation='none')
            if title is not None:
                plt.title(title)
    plt.tight_layout()
def flat(dataset):
    flatten_dataset = dataset[:,].flatten()
    return flatten_dataset.reshape(
        dataset.shape[0], 
        dataset.shape[1] * dataset.shape[2] * dataset.shape[3],
    )

def onehot(dataset):
    return np.eye(10)[dataset.reshape(-1)]

(x_train, y_train), (x_test, y_test) = cifar10.load_data()
cifar10_categories = [
    'airplane',
    'automobile',
    'bird',
    'cat',
    'deer',
    'dog',
    'frog',
    'horse',
    'ship',
    'truck',
]

# TODO: why do we need this? 
# Got from https://github.com/fchollet/keras/blob/master/examples/cifar10_cnn.py#L65
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255

# prepare train and test samples (make it flat)
x_train_flatten = flat(x_train)
x_test_flatten = flat(x_test)

# # make one-hot y (out put) labels
# # y_train_onehot = onehot(y_train)
y_train_onehot = keras.utils.to_categorical(y_train, num_classes=10)
# # y_test_onehot = onehot(y_test)
y_test_onehot = keras.utils.to_categorical(y_test, num_classes=10)

# print(x_train.shape)
# print(x_train_flatten.shape)
# print(y_train.shape)
# print(y_train_onehot.shape)
    
# def get_images_by_category(category_idx):
#     return x_train[np.where(y_train == category_idx)[0]]

# def get_image(category_idx, image_idx):
#     return get_images_by_category(category_idx)[image_idx], cifar10_categories[category_idx] if image_idx == 0 else None

# grid_of_images(5, len(cifar10_categories), get_image, grid_width=8)

def get_images_by_category(category_idx):
    return x_train[np.where(y_train == category_idx)[0]]

def get_image(category_idx, image_idx):
    return get_images_by_category(category_idx)[image_idx], cifar10_categories[category_idx] if image_idx == 0 else None

# grid_of_images(5, len(cifar10_categories), get_image, grid_width=8)<jupyter_output><empty_output><jupyter_text>Model  Build<jupyter_code>model = Sequential()
# model.add(Dense(units=32, activation='relu', input_shape=x_train_flatten.shape[1:]))
# model.add(Dropout(0.5))
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=x_train.shape[1:]))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Flatten())

model.add(Dense(512))
model.add(Dropout(0.5))
model.add(Dense(units=10, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adam())<jupyter_output><empty_output><jupyter_text>Train<jupyter_code># model.fit(x_train_flatten, y_train_onehot, epochs=5, batch_size=32,
#          validation_data=(x_test_flatten, y_test_onehot))

# interrupt training when the validation loss isn't decreasing anymore 
early_stopping = EarlyStopping(monitor='val_loss', patience=2)

hist = model.fit(x_train, y_train_onehot, epochs=3, batch_size=32, 
                 callbacks=[early_stopping], validation_data=(x_test, y_test_onehot))

print(hist.history)<jupyter_output>Train on 50000 samples, validate on 10000 samples
Epoch 1/3
50000/50000 [==============================] - 209s 4ms/step - loss: 1.5685 - val_loss: 1.2241
Epoch 2/3
50000/50000 [==============================] - 203s 4ms/step - loss: 1.2183 - val_loss: 1.1151
Epoch 3/3
50000/50000 [==============================] - 194s 4ms/step - loss: 1.0819 - val_loss: 1.0045
{'val_loss': [1.2241160591125488, 1.1150799087524415, 1.004454746055603], 'loss': [1.5684755651283264, 1.2182583550643922, 1.081866922454834]}<jupyter_text>Accuracy<jupyter_code># TODO:
# GOT strange bug - evaluate stuck after batch_size iteration. so if our test test has more 
# than batch_size samples it won't never be evaluated.
#
# For the last model it works. 
# So Maybe I got it because didn't divide input vectors on 255 and got overflow

# Score trained model.
model.evaluate(x_test, y_test_onehot, verbose=1)<jupyter_output>10000/10000 [==============================] - 10s 1ms/step<jupyter_text>Pre-trained Modelshttps://keras.io/applications/ ResNet50TODO: API to fetch images from internet (Google Image Search, 500px, ...) for trying prediction<jupyter_code>from sklearn.datasets import load_sample_images

from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np

model = ResNet50(weights='imagenet')

# img_path = 'elephant.jpg'
# img = image.load_img(img_path, target_size=(224, 224))
# x = image.img_to_array(img)
# x = np.expand_dims(x, axis=0)

# scikit-learn images
from PIL import Image
im = Image.fromarray(np.uint8(load_sample_images().images[1]))
im = im.resize((224, 224))
x = np.array(im, dtype=np.float)

# random pixels
# x = np.floor(np.random.random((224, 224, 3)) * 255) 


x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

preds = model.predict(x)
# decode the results into a list of tuples (class, description, probability)
# (one such list for each sample in the batch)
plt.imshow(im)
print('Predicted:', decode_predictions(preds, top=3)[0])
# Predicted: [(u'n02504013', u'Indian_elephant', 0.82658225), (u'n01871265', u'tusker', 0.1122357), (u'n02504458', u'African_elephant', 0.061040461)]
decode_predictions(model.predict(x), top=5)[0]<jupyter_output><empty_output><jupyter_text>VGG16<jupyter_code>from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
import numpy as np

model = VGG16(weights='imagenet', include_top=False)

# scikit-learn images
from PIL import Image
im = Image.fromarray(np.uint8(load_sample_images().images[1]))
im = im.resize((224, 224))
x = np.array(im, dtype=np.float)

# img_path = 'elephant.jpg'
# img = image.load_img(img_path, target_size=(224, 224))
# x = image.img_to_array(img)

x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

features = model.predict(x)
features.shape<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14637030030740128, 'fraction_numerical': 0.055805154882951055, 'mean_word_length': 5.384150943396226, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 30, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '626270', 'license': 'MIT', 'n_tokens_mistral': 3423, 'n_tokens_neox': 3096, 'n_words': 630, 'path': 'main.ipynb', 'repo_name': 'hyzhak/keras-experiments', 'size': '8467'}