Skip to content

Commit 95cdbbc

Browse files
committed
Make transfer work with tf2
1 parent 287a0bc commit 95cdbbc

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

examples/keras-transfer/dogcat-transfer-and-finetune.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ def setup_to_finetune(model):
7070
# include_top=False excludes final FC layer
7171
base_model = InceptionV3(weights='imagenet', include_top=False, pooling="avg")
7272
model = add_new_last_layer(base_model, nb_classes)
73-
model._is_graph_network = False
7473

7574
# fine-tuning
7675
setup_to_finetune(model)

examples/keras-transfer/dogcat-transfer-dense.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414

1515
run = wandb.init()
1616
config = run.config
17-
#fixed size for DenseNet121
17+
# fixed size for DenseNet121
1818
config.img_width = 224
1919
config.img_height = 224
2020
config.epochs = 10
21-
config.batch_size = 128
21+
config.batch_size = 32
22+
2223

2324
def setup_to_transfer_learn(model, base_model):
2425
"""Freeze all layers and compile the model"""
@@ -30,7 +31,9 @@ def setup_to_transfer_learn(model, base_model):
3031
epsilon=None,
3132
decay=0.0)
3233
sgd = SGD(lr=0.001, momentum=0.9)
33-
model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy'])
34+
model.compile(optimizer=sgd, loss='categorical_crossentropy',
35+
metrics=['accuracy'])
36+
3437

3538
def add_new_last_layer(base_model, nb_classes, activation='softmax'):
3639
"""Add last layer to the convnet
@@ -43,13 +46,15 @@ def add_new_last_layer(base_model, nb_classes, activation='softmax'):
4346
predictions = Dense(nb_classes, activation=activation)(base_model.output)
4447
return Model(inputs=base_model.input, outputs=predictions)
4548

49+
4650
train_dir = "dogcat-data/train"
4751
val_dir = "dogcat-data/validation"
4852
nb_train_samples = get_nb_files(train_dir)
4953
nb_classes = len(glob.glob(train_dir + "/*"))
5054
nb_val_samples = get_nb_files(val_dir)
5155

52-
train_generator, validation_generator = generators(preprocess_input, config.img_width, config.img_height, config.batch_size)
56+
train_generator, validation_generator = generators(
57+
preprocess_input, config.img_width, config.img_height, config.batch_size)
5358

5459
# setup model
5560
base_model = DenseNet121(input_shape=(config.img_width, config.img_height, 3),
@@ -69,7 +74,8 @@ def add_new_last_layer(base_model, nb_classes, activation='softmax'):
6974
steps_per_epoch=nb_train_samples * 2 / config.batch_size,
7075
validation_data=validation_generator,
7176
validation_steps=nb_train_samples / config.batch_size,
72-
callbacks=[WandbCallback(data_type="image", generator=validation_generator, labels=['cat', 'dog'], save_model=False)],
77+
callbacks=[WandbCallback(data_type="image", generator=validation_generator, labels=[
78+
'cat', 'dog'], save_model=False)],
7379
class_weight='auto')
7480

7581
model.save("transfered.h5")

examples/keras-transfer/dogcat-transfer.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
config.img_height = 299
1919
config.epochs = 5
2020
config.fc_size = 1024
21-
config.batch_size = 128
21+
config.batch_size = 64
2222

2323

2424
def setup_to_transfer_learn(model, base_model):
@@ -60,7 +60,6 @@ def add_new_last_layer(base_model, nb_classes):
6060
# setup model
6161
base_model = InceptionV3(weights='imagenet', include_top=False, pooling="avg")
6262
model = add_new_last_layer(base_model, nb_classes)
63-
model._is_graph_network = False
6463

6564
# transfer learning
6665
setup_to_transfer_learn(model, base_model)

0 commit comments

Comments
 (0)