This file contains 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
# hyp: org_model is VGG16 | |
def create_base_model( org_model, input_shape ): | |
base_model = Sequential() | |
l = org_model.get_layer('block1_conv1') | |
base_model.add( Conv2D( l.filters, l.kernel_size, strides=l.strides, padding=l.padding, | |
use_bias=l.use_bias, data_format=l.data_format, activation=l.activation, | |
name='my_block1_conv1', | |
input_shape=input_shape ) ) |
This file contains 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
def create_content_mod( base_model, input_shape ): | |
input_c = Input( shape=input_shape ) | |
m4 = Model(inputs=base_model.input, outputs=base_model.get_layer('my_block4_conv2').output) | |
c4 = m4(input_c) | |
c4 = Flatten()(c4) | |
m5 = Model(inputs=base_model.input, outputs=base_model.get_layer('my_block5_conv2').output) | |
c5 = m5(input_c) | |
c5 = Flatten()(c5) |
This file contains 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
def create_blockstyle( base_model, layer_name, input_shape ): | |
mod = Model(inputs=base_model.input, outputs=base_model.get_layer(layer_name).output) | |
oshape = K.int_shape(mod.output) | |
w,c = oshape[1],oshape[-1] | |
x = Input( shape=input_shape ) | |
y = mod(x) | |
y = Reshape( (w*w, c) )(y) | |
z = Dot( 1 )( [y, y] ) |
This file contains 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
def create_merge_model( input_shape, style_mod, content_mod ): | |
input_m = Input(shape=(1,)) | |
W,H,C = input_shape[0],input_shape[1],input_shape[2] | |
x = Dense(W*H*C, use_bias=False )(input_m) | |
x = Reshape((W,H,C))(x) | |
s_x = style_mod(x) | |
c_x = content_mod(x) | |
mod = Model(inputs=[input_m], outputs=[s_x,c_x]) |
This file contains 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
input_shape = (W,W,3) | |
base_model = create_base_model( org_model, input_shape ) | |
style_mod = create_style_mod( base_model, input_shape ) | |
content_mod = create_content_mod( base_model, input_shape ) | |
mod = create_merge_model( input_shape, style_mod, content_mod ) | |
style_target = style_mod.predict(S) | |
content_target = content_mod.predict(C) |
This file contains 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 os | |
import numpy as np | |
np.random.seed(os.getpid()) #1337) # for reproducibility | |
from tqdm import tqdm | |
from tqdm import trange | |
import matplotlib | |
matplotlib.use('Agg') | |
import matplotlib.pyplot as plt |
This file contains 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
mod = MobileNet(weights='imagenet', input_shape=(224, 224, 3)) | |
img_path = 'cat.jpeg' | |
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) | |
preds = mod.predict(x) |
This file contains 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.models import Sequential | |
mod = MobileNet(weights='imagenet', input_shape=(224, 224, 3)) | |
model = Sequential() | |
model.add( mod ) | |
preds = model.predict(x) | |
class_idx = np.argmax(preds[0]) | |
class_output = model.output[:, class_idx] |
This file contains 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 os | |
import numpy as np | |
from keras.datasets import mnist | |
# load data and reshape the Tensors | |
(X_train, y_train), (X_test, y_test) = mnist.load_data() | |
X_train = X_train.astype(np.float32).reshape((X_train.shape[0],28,28)) / 255.0 | |
X_test = X_test.astype(np.float32).reshape((X_test.shape[0],28,28)) / 255.0 |
This file contains 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.layers import (Conv2D, BatchNormalization, Activation, Flatten) | |
# Build a model with 14 output nodes | |
model = Sequential() | |
model.add( Conv2D(8, (3,3), padding='same', input_shape=(28,28,1))) | |
model.add( BatchNormalization() ) | |
model.add( Activation('relu') ) | |
OlderNewer