This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
history = network.fit(train_images, train_labels, epochs=5, batch_size=128, validation_data=(val_images, val_labels)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# download data | |
from keras.datasets import mnist | |
(train_images, train_labels), (val_images, val_labels) = mnist.load_data() | |
# flatten and scale images | |
train_images = train_images.reshape((-1, 28*28)).astype('float32') / 255 | |
val_images = val_images.reshape((-1, 28*28)).astype('float32') / 255 | |
# one-hot encode the labels | |
train_labels, val_labels = keras.utils.to_categorical(train_labels), keras.utils.to_categorical(val_labels) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras import models | |
from keras import layers | |
network = models.Sequential() | |
network.add(layers.Dense(512, activation='relu', input_shape=(28 * 28, ))) | |
network.add(layers.Dense(256, activation='relu')) | |
network.add(...) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
model = tf.keras.Model(inputs=image_input, outputs=[weather_output, ground_output]) | |
model.compile(optimizer='adam', | |
loss={'weather': 'categorical_crossentropy', | |
'ground': 'binary_crossentropy'}) | |
history = model.fit(ds_train, | |
steps_per_epoch=100, # let's take just a couple of steps | |
epochs=1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Serialize images, together with labels, to TF records | |
def _bytes_feature(value): | |
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) | |
tf_records_filename = './data/KagglePlanetTFRecord_{}'.format(IM_SIZE) | |
writer = tf.python_io.TFRecordWriter(tf_records_filename) | |
# List of image paths, np array of labels | |
im_list = [os.path.join('./data/train', v + '.jpg') for v in df_train['image_name'].tolist()] | |
w_labels_arr = np.array([ast.literal_eval(l) for l in df_train['weather_labels']]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
# The export path contains the name and the version of the model | |
tf.keras.backend.set_learning_phase(0) # Ignore dropout at inference | |
model = tf.keras.models.load_model('./model.h5') | |
export_path = './PlanetModel/1' | |
# Fetch the Keras session and save the model | |
# The signature definition is defined by the input and output tensors | |
# And stored with the default serving key |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with tf.Session() as sess: | |
tf.keras.backend.set_session(sess) | |
sess.run(ds_tr_init) # initialize the generator | |
# Rewire network to tie it into the generator | |
image_input = tf.keras.Input(tensor=x) | |
# Model definition | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
featdef = { | |
'image': tf.FixedLenFeature(shape=[], dtype=tf.string), | |
'weather_labels': tf.FixedLenFeature(shape=[], dtype=tf.string), | |
'ground_labels': tf.FixedLenFeature(shape=[], dtype=tf.string) | |
} | |
def _parse_record(example_proto, clip=False): | |
"""Parse a single record into image, weather labels, ground labels""" | |
example = tf.parse_single_example(example_proto, featdef) | |
im = tf.decode_raw(example['image'], tf.float32) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
len(predictions[1]) == len(df_train) # Total number of images in dataset |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test_seq = KagglePlanetSequence('./KagglePlanetMCML.csv', | |
'./data/train/', | |
im_size=IM_SIZE, | |
batch_size=32, mode='test') | |
predictions = model.predict_generator(generator=test_seq, verbose=1) | |
len(predictions[1]) == len(df_train) # This is True! |