def test_time_distributed(self): input_time = Input(shape=(None, ), name='input_time') emb = Embedding(10, 4, name='embedding')(input_time) lstm = LSTM(4, name='lstm')(emb) output_time = Dense(1, name='output_time')(lstm) model_time = Model(input_time, output_time) print(model_time.summary()) # define full model input = Input(shape=(None, None), name='input') timesteps = TimeDistributed(model_time, name='timedistributed')(input) output = Dense(1)(timesteps) model = Model(input, output) # create example x = np.array([[[1, 2, 1]], [[3, 4, 5]]]) # get activations acts = keract.get_activations(model, x, layer_names=['timedistributed'], nested=True) self.assertTrue('timedistributed' in acts)
def test_custom_loss(self): x = np.random.rand(100).reshape(100, 1) inputs = tf.keras.layers.Input(shape=(1, )) d = tf.keras.layers.Dense(11)(inputs) d = tf.keras.layers.Dense(1)(d) model = tf.keras.Model(inputs=inputs, outputs=d) def loss(y_true, y_pred): return tf.reduce_mean((y_true - y_pred)**2) optimizer = tf.keras.optimizers.Adam() model.add_loss(loss(inputs, d)) model.compile(optimizer) keract.get_activations(model, x)
def test_model_in_model(self): np.random.seed(123) inputs = np.random.uniform(size=(8, 32, 32, 1)) nested_model = create_network_with_one_subnet() # will get the activations of every layer, EXCLUDING subnet layers. acts_not_nested = keract.get_activations(nested_model, inputs) self.assertTrue('subnet' in acts_not_nested) self.assertTrue('subnet/conv2d' not in acts_not_nested) # will get the activations of every layer, including subnet. acts_nested = keract.get_activations(nested_model, inputs, nested=True) self.assertTrue('subnet' not in acts_nested) self.assertTrue('subnet/conv2d' in acts_nested) self.assertTrue('subnet/max_pooling2d' in acts_nested) self.assertTrue('subnet/conv2d_1' in acts_nested) self.assertTrue('subnet/max_pooling2d_1' in acts_nested)
def test_multi_inputs_multi_outputs(self): inp_a = np.random.uniform(size=(5, 10)) inp_b = np.random.uniform(size=(5, 10)) # out_d = np.random.uniform(size=(5, 1)) # out_e = np.random.uniform(size=(5, 1)) m1 = get_multi_outputs_model() m1.compile(optimizer='adam', loss='mse') # m1.fit(x=[inp_a, inp_b], y=[out_d, out_e]) acts = keract.get_activations(m1, [inp_a, inp_b]) self.assertListEqual(list(acts['i1'].shape), [5, 10]) self.assertListEqual(list(acts['i2'].shape), [5, 10]) self.assertListEqual(list(acts['add'].shape), [5, 10]) self.assertListEqual(list(acts['o1'].shape), [5, 1]) self.assertListEqual(list(acts['o2'].shape), [5, 2])
def test_get_activations_from_multi_outputs(self): # define model inputs = tf.keras.Input(shape=(None, ), name='input') emb = tf.keras.layers.Embedding(100, 4, name='embeddding')(inputs) lstm = tf.keras.layers.LSTM(4, return_sequences=True, return_state=True, name='lstm') lstm_outputs, state_h, state_c = lstm(emb) outputs = tf.keras.layers.Dense(1, name='final_dense')(state_h) model = tf.keras.Model(inputs, outputs) model.summary() # create example x = np.ones(shape=(16, 10)) # get activations act = keract.get_activations(model, x, layer_names='lstm')['lstm'] self.assertEqual(len(act), 3) self.assertListEqual(list(act[0].shape), [16, 10, 4]) self.assertListEqual(list(act[1].shape), [16, 4]) self.assertListEqual(list(act[2].shape), [16, 4])
from tensorflow.keras.preprocessing.image import img_to_array from Models import cifar10_modelfn as cf # Check for GPUs and set them to dynamically grow memory as needed # Avoids OOM from tensorflow greedily allocating GPU memory model = cf.load_weights() # url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/14/Gatto_' \ # 'europeo4.jpg/250px-Gatto_europeo4.jpg' # response = requests.get(url) # image = Image.open(BytesIO(response.content)) # image = image.crop((0, 0, 224, 224)) image = Image.open('Visualizations/dog10.png') image = img_to_array(image) image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) image = preprocess_input(image) # yhat = model.predict(image) # label = decode_predictions(yhat) # label = label[0][0] # print('{} ({})'.format(label[1], label[2] * 100)) # # model.compile(optimizer='adam', # loss='categorical_crossentropy', # metrics=['accuracy']) activations = keract.get_activations(model, image) first = activations.get('block1_conv1') keract.display_activations(activations, save=True)