<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Reducing the size of the data to test on a cpu
Step1:  Splitting the data into validation and train + One Hot Encoding the labels + data augumentation
Step2:  Recizing the images into appropriate dimensions that VGG16 expects (48x48 - 224x224)
Step3:  Plotting sample resized image and it's augumented copies
Step4:  Barebones Convolutional model
Step5:  Using VGG16 Bottleneck Features
<ASSISTANT_TASK:>
Python Code:
def reducex(X, y, reduce_classes=None, reduce_percent=.2):
#     import pdb; pdb.set_trace()
    idxs = []
    if reduce_classes:
        for c in reduce_classes:
            try:
                idxs += list(np.where(y == c)[0])
            except IndexError:
                continue
    np.random.seed(1000)
    new_size = int(np.round(len(idxs) * reduce_percent))
    np.random.shuffle(idxs)
    return (X[idxs[:int(len(idxs)*reduce_percent)]], y[idxs[:int(len(idxs)*reduce_percent)]] == 3)
batch_size = 16
Explanation: Reducing the size of the data to test on a cpu
End of explanation
(Xtrain, ytrain), (Xtest, ytest) = cifar10.load_data()
train_datagen = ImageDataGenerator(
            rotation_range=40,
            width_shift_range=0.2,
            height_shift_range=0.2,
            shear_range=0.2,
            zoom_range=0.2,
            horizontal_flip=True,
            fill_mode='nearest')
test_datagen = ImageDataGenerator(rescale=1./255)
Xtrain, ytrain = reducex(Xtrain, ytrain, reduce_classes=[3,5], reduce_percent=0.3)
Xtest, ytest = reducex(Xtest, ytest, reduce_classes=[3,5], reduce_percent=0.3)
yetrain = keras.utils.to_categorical(ytrain)
yetest = keras.utils.to_categorical(ytest)
Explanation: Splitting the data into validation and train + One Hot Encoding the labels + data augumentation
End of explanation
######## Resize cifar10 images to 3x48x48 #############
rsize = 48
temp = np.zeros((Xtrain.shape[0], 3, rsize, rsize))
for i, row in enumerate(Xtrain):
    temp[i] = imresize(row, size=(rsize,rsize)).transpose(2,1,0)
temp2 = np.zeros((Xtrain.shape[0], 3, rsize, rsize))
for i in Xtest:
    temp2[i] = imresize(row, size=(rsize,rsize)).transpose(2,1,0)
Xtrain = temp
Xtest = temp2
Explanation: Recizing the images into appropriate dimensions that VGG16 expects (48x48 - 224x224)
End of explanation
from keras.applications import VGG16
vmodel = VGG16(include_top=True, weights='imagenet')#, input_shape=(3,48,48))
vmodel.layers.pop()
for layr in vmodel.layers:
    layr.trainable = False
last = vmodel.output
# x = Flatten(last)
x = Dense(2, activation='sigmoid')(last)
vvmodel = keras.models.Model(vmodel.input, x)
# vmodel.add(Dense(2))
# vmodel.add(Activation('sigmoid'))
vvmodel.summary()
opt = RMSprop(lr=0.01)
vvmodel.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
def fit_model(model, batches, val_batches, epochs=1):
    model.fit_generator(batches, steps_per_epoch=1500 // batch_size, epochs=epochs, 
                        validation_steps=700 // batch_size,
                        validation_data=val_batches)
vvmodel.fit(Xtrain[0:100], yetrain[0:100], verbose=True)
fit_model(vvmodel, train_generator, validation_generator, epochs=2)
# model.fit_generator(
#         train_generator,
#         steps_per_epoch=500 // batch_size,
#         epochs=15,
#         validation_data=validation_generator,
#         validation_steps=300 // batch_size)
# img = load_img('data/train/cats/cat.0.jpg')  # this is a PIL image
# x = img_to_array(img)  # this is a Numpy array with shape (3, 150, 150)
x = Xtrain[8]
x = x.reshape((1,) + x.shape)  # this is a Numpy array with shape (1, 3, 150, 150)
# the .flow() command below generates batches of randomly transformed images
# and saves the results to the `preview/` directory
i = 0
j = 0
xims = []
fig, axes = plt.subplots(2,2,figsize=(12,8))
for batch in datagen.flow(x, batch_size=1):
    axes[j][i].set_axis_off()
    axes[j][i].imshow(batch.reshape(x.shape[1:]).transpose(2,1,0), interpolation='nearest')
    if i >= 1:
        j += 1
        i = -1
    if j == 2:
        break
    i +=1 
    print j,i
    
# plt.imshow(Xtrain[0].reshape(32,32,3))
# fig, axes1 = plt.subplots(2,2,figsize=(12,8))
# for j in range(5):
#     for k in range(5):
#         i = np.random.choice(range(len(X)))
#         axes1[j][k].set_axis_off()
#         axes1[j][k].imshow(X[i:i+1][0], interpolation='nearest')
print ytrain[8]
# train_generator = datagen.flow(Xtrain,yetrain,
#         batch_size=batch_size)  # since we use binary_crossentropy loss, we need binary labels
# # this is a similar generator, for validation data
# validation_generator = test_datagen.flow(Xtest, yetest,
#         batch_size=batch_size)
Explanation: Plotting sample resized image and it's augumented copies
End of explanation
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(3, 32, 32)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten()) # this converts our 3D feature maps to 1D feature vectors
model.add(Dense(32))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(2))
model.add(Activation('sigmoid'))
print(model.summary())
model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])
batch_size = 64
model.fit_generator(
        train_generator,
        steps_per_epoch=500 // batch_size,
        epochs=15,
        validation_data=validation_generator,
        validation_steps=300 // batch_size)
import datetime
now = str(datetime.datetime.now())
# model.save_weights('first_try.h5')  # always save your weights after training or during training
weights = model.weights[6:]
weights[0].eval().shape
Explanation: Barebones Convolutional model
End of explanation
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow(Xtrain,
        batch_size=batch_size, shuffle=True)  # since we use binary_crossentropy loss, we need binary labels
# this is a similar generator, for validation data
validation_generator = test_datagen.flow(Xtest,
        batch_size=batch_size, shuffle=True)
# train_batches = train_generator.flow(Xtrain, yetrain, batch_size=batch_size, shuffle=True)
# val_batches = test_datagen.flow(Xtest, yetest, batch_size=batch_size, shuffle=False)
bottleneck_features_train = vmodel.predict_generator(
        train_generator, 500)
np.save(open('bottleneck_features_train.npy', 'w'),
            bottleneck_features_train)
top_model = Sequential()
top_model.add(Flatten(input_shape=(256,32),weights=np.zeros((256,32))))
top_model.add(Dense(32))
top_model.add(Activation('relu'))
top_model.add(Dropout(0.5))
top_model.add(Dense(2))
top_model.add(Activation('sigmoid'))
# top_model.set_weights(weights)
top_model.layers[0].set_weights(weights[0].eval())
top_model_weights_path = 'first_try.h5'
top_model.load_weights(top_model_weights_path)
# add the model on top of the convolutional base
vmodel.add(top_model)
# set the first 25 layers (up to the last conv block)
# to non-trainable (weights will not be updated)
for layer in model.layers[:25]:
    layer.trainable = False
# compile the model with a SGD/momentum optimizer
# and a very slow learning rate.
vmodel.compile(loss='binary_crossentropy',
              optimizer=SGD(lr=1e-4, momentum=0.9),
              metrics=['accuracy'])
# prepare data augmentation configuration
# train_datagen = ImageDataGenerator(
#     rescale=1. / 255,
#     shear_range=0.2,
#     zoom_range=0.2,
#     horizontal_flip=True)
# test_datagen = ImageDataGenerator(rescale=1. / 255)
# train_generator = train_datagen.flow_from_directory(
#     train_data_dir,
#     target_size=(img_height, img_width),
#     batch_size=batch_size,
#     class_mode='binary')
# validation_generator = test_datagen.flow_from_directory(
#     validation_data_dir,
#     target_size=(img_height, img_width),
#     batch_size=batch_size,
#     class_mode='binary')
# fine-tune the model
epochs = 25
vmodel.fit_generator(
    train_generator,
    samples_per_epoch=nb_train_samples,
    epochs=epochs,
    validation_data=validation_generator,
    nb_val_samples=nb_validation_samples)
Explanation: Using VGG16 Bottleneck Features
End of explanation
<END_TASK>