i am trying to run the python repository for digit classification using Mnist dataset in nengo but unable to get the results due to this error "AttributeError: 'Conv2D' object has no attribute 'subsample" i tried hard to get rid of this error but failed any one who can suggest me the solution to this error.
from __future__ import print_function
import os
os.environ['THEANO_FLAGS'] = 'device=gpu,floatX=float32'
import nengo
import nengo_ocl
import numpy as np
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import (
Dense, Dropout, Activation, Flatten, Convolution2D, AveragePooling2D)
from keras.layers.noise import GaussianNoise
from keras.utils import np_utils
import nengo
from nengo_extras.keras import (
load_model_pair, save_model_pair, SequentialNetwork, SoftLIF)
from nengo_extras.gui import image_display_function
np.random.seed(1)
filename = 'mnist_spiking_cnn'
# --- Load data
img_rows, img_cols = 28, 28
nb_classes = 10
# the data, shuffled and split between train and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)
X_train = X_train.astype('float32')/128 - 1
X_test = X_test.astype('float32')/128 - 1
# --- Train model
if not os.path.exists(filename + '.h5'):
batch_size = 128
nb_epoch = 6
# number of convolutional filters to use
nb_filters = 32
# size of pooling area for max pooling
nb_pool = 2
# convolution kernel size
nb_conv = 3
# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
kmodel = Sequential()
softlif_params = dict(
sigma=0.002, amplitude=0.063, tau_rc=0.022, tau_ref=0.002)
kmodel.add(GaussianNoise(0.1, input_shape=(1, img_rows, img_cols)))
kmodel.add(Convolution2D(nb_filters, nb_conv, nb_conv, border_mode='valid'))
kmodel.add(SoftLIF(**softlif_params))
kmodel.add(Convolution2D(nb_filters, nb_conv, nb_conv))
kmodel.add(SoftLIF(**softlif_params))
kmodel.add(AveragePooling2D(pool_size=(nb_pool, nb_pool)))
kmodel.add(Dropout(0.25))
kmodel.add(Flatten())
kmodel.add(Dense(128))
kmodel.add(SoftLIF(**softlif_params))
kmodel.add(Dropout(0.5))
kmodel.add(Dense(nb_classes))
kmodel.add(Activation('softmax'))
kmodel.compile(loss='categorical_crossentropy',
optimizer='adadelta',
metrics=['accuracy'])
kmodel.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
verbose=1, validation_data=(X_test, Y_test))
score = kmodel.evaluate(X_test, Y_test, verbose=0)
print('Test score:', score[0])
print('Test accuracy:', score[1])
save_model_pair(kmodel, filename, overwrite=True)
else:
kmodel = load_model_pair(filename)
# --- Run model in Nengo
presentation_time = 0.2
model = nengo.Network()
with model:
u = nengo.Node(nengo.processes.PresentInput(X_test, presentation_time))
knet = SequentialNetwork(kmodel, synapse=nengo.synapses.Alpha(0.005))
nengo.Connection(u, knet.input, synapse=None)
input_p = nengo.Probe(u)
output_p = nengo.Probe(knet.output)
# --- image display
image_shape = kmodel.input_shape[1:]
display_f = image_display_function(image_shape)
display_node = nengo.Node(display_f, size_in=u.size_out)
nengo.Connection(u, display_node, synapse=None)
# --- output spa display
vocab_names = ['ZERO', 'ONE', 'TWO', 'THREE', 'FOUR',
'FIVE', 'SIX', 'SEVEN', 'EIGHT', 'NINE']
vocab_vectors = np.eye(len(vocab_names))
vocab = nengo.spa.Vocabulary(len(vocab_names))
for name, vector in zip(vocab_names, vocab_vectors):
vocab.add(name, vector)
config = nengo.Config(nengo.Ensemble)
config[nengo.Ensemble].neuron_type = nengo.Direct()
with config:
output = nengo.spa.State(len(vocab_names), subdimensions=10, vocab=vocab)
nengo.Connection(knet.output, output.input)
the error trace is below
AttributeError Traceback (most recent call last)
<ipython-input-14-6831ac9971de> in <module>()
5 with model:
6 u = nengo.Node(nengo.processes.PresentInput(X_train, presentation_time))
----> 7 knet = SequentialNetwork(kmodel,synapse=nengo.synapses.Alpha(0.001))
8 nengo.Connection(u, knet.input, synapse=None)
9
~\Anaconda3.0\lib\site-packages\nengo_extras\keras.py in __init__(self, model, synapse, lif_type, **kwargs)
79 self.add_data_layer(np.prod(model.input_shape[1:]))
80 for layer in model.layers:
---> 81 self._add_layer(layer)
82
83 def _add_layer(self, layer):
~\Anaconda3.0\lib\site-packages\nengo_extras\keras.py in _add_layer(self, layer)
99 for cls in type(layer).__mro__:
100 if cls in layer_adder:
--> 101 return layer_adder[cls](layer)
102
103 raise NotImplementedError("Cannot build layer type %r" %
~\Anaconda3.0\lib\site-packages\nengo_extras\keras.py in _add_conv2d_layer(self, layer)
112 filters, biases = layer.get_weights()
113 filters = filters[..., ::-1, ::-1] # flip
--> 114 strides = layer.subsample
115
116 nf, nc, ni, nj = filters.shape
AttributeError: 'Conv2D' object has no attribute 'subsample'
See this https://forums.fast.ai/t/what-is-subsample-of-convolution2d-doing/3555
which suggests that "subsample" in old versions of keras has been replaced by "strides" in new versions
Related
I have done simple transfer learning for a custom dataset using a mobilenetv2 model available in Tensorflow Hub. However I'm getting this error when I try to do Quantization aware training. I don't understand how to solve it exactly. What should I be doing?
Below is my code and the error I'm getting
import numpy as np
import time
import PIL.Image as Image
import matplotlib.pylab as plt
import tensorflow as tf
from tensorflow import keras
from keras.preprocessing.image import ImageDataGenerator
import tensorflow_hub as hub
import datetime
from google.colab import drive
drive.mount('/content/gdrive', force_remount=True)
dataset_path = '/content/gdrive/MyDrive/images/'
image_size = (224,224)
batch_size = 10
train_datagen = ImageDataGenerator(rescale=1./255,
rotation_range=15,
zoom_range = (0.95,0.95),
width_shift_range=0.1,
height_shift_range=0.1,
validation_split=0.2,
dtype = tf.float32,
)
validation_datagen = ImageDataGenerator(rescale=1./255, validation_split=0.2, dtype = tf.float32,)
train_batches = train_datagen.flow_from_directory(
dataset_path,
target_size = image_size,
batch_size = batch_size,
color_mode='rgb',
class_mode = 'categorical',
shuffle = True,
seed = 123,
subset = 'training', )
validation_batches = validation_datagen.flow_from_directory(
dataset_path,
target_size = image_size,
batch_size = batch_size,
color_mode='rgb',
class_mode = 'categorical',
shuffle = True,
seed = 123,
subset = 'validation', )
test_batches = validation_datagen.flow_from_directory(
dataset_path,
target_size = image_size,
batch_size = batch_size,
color_mode='rgb',
class_mode = 'categorical',
shuffle = True,
seed = 123,
subset = 'validation', )
from keras import layers
import tensorflow_hub as hub
url = "https://tfhub.dev/google/experts/bit/r50x1/in21k/bird/1"
classifier_model = url
base_model = hub.KerasLayer(classifier_model, input_shape=image_size+(3,), trainable=False)
num_of_birds = 10
model = tf.keras.Sequential([
base_model,
tf.keras.layers.Dense(num_of_birds, activation='softmax'),
])
model.summary()
model.compile(optimizer=keras.optimizers.Adam(), loss='categorical_crossentropy', metrics=["accuracy"],)
model.fit_generator(train_batches, epochs=5)
model.evaluate(test_batches)
import tensorflow_model_optimization as tfmot
quantize_model = tfmot.quantization.keras.quantize_model
q_aware_model = quantize_model(model)
I get this error right when I try to run the above line of code
ValueError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/tensorflow_model_optimization/python/core/quantization/keras/quantize.py in quantize_apply(model, scheme)
442 try:
--> 443 model_copy = _clone_model_with_weights(model)
444 except ValueError as er:
12 frames
ValueError: Unknown layer: KerasLayer. Please ensure this object is passed to the `custom_objects` argument. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/tensorflow_model_optimization/python/core/quantization/keras/quantize.py in quantize_apply(model, scheme)
447 'Keras layers or objects in your model. Please specify them via '
448 '`quantize_scope` for your calls to `quantize_model` and '
--> 449 '`quantize_apply`. [%s].' % er)
450
451 # 2. Remove QuantizeAnnotate wrappers from the layers in the model. This
ValueError: Unable to clone model. This generally happens if you used custom Keras layers or objects in your model. Please specify them via `quantize_scope` for your calls to `quantize_model` and `quantize_apply`. [Unknown layer: KerasLayer. Please ensure this object is passed to the `custom_objects` argument. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.].
```import numpy as np
import glob
import os
from keras.models import Model
from keras.layers import Input, Dense, GRU, CuDNNGRU, CuDNNLSTM
from keras import optimizers
import h5py
from sklearn.model_selection import train_test_split
from keras.models import load_model
def language_name(index):
if index == 0:
return "English"
elif index == 1:
return "Hindi"
elif index == 2:
return "Mandarin"
# ---------------------------BLOCK 1------------------------------------
# COMMENT/UNCOMMENT BELOW CODE BLOCK -
# Below code extracts mfcc features from the files provided into a dataset
codePath = './train/'
num_mfcc_features = 64
english_mfcc = np.array([]).reshape(0, num_mfcc_features)
for file in glob.glob(codePath + 'english/*.npy'):
current_data = np.load(file).T
english_mfcc = np.vstack((english_mfcc, current_data))
hindi_mfcc = np.array([]).reshape(0, num_mfcc_features)
for file in glob.glob(codePath + 'hindi/*.npy'):
current_data = np.load(file).T
hindi_mfcc = np.vstack((hindi_mfcc, current_data))
mandarin_mfcc = np.array([]).reshape(0, num_mfcc_features)
for file in glob.glob(codePath + 'mandarin/*.npy'):
current_data = np.load(file).T
mandarin_mfcc = np.vstack((mandarin_mfcc, current_data))
# Sequence length is 10 seconds
sequence_length = 1000
list_english_mfcc = []
num_english_sequence = int(np.floor(len(english_mfcc)/sequence_length))
for i in range(num_english_sequence):
list_english_mfcc.append(english_mfcc[sequence_length*i:sequence_length*(i+1)])
list_english_mfcc = np.array(list_english_mfcc)
english_labels = np.full((num_english_sequence, 1000, 3), np.array([1, 0, 0]))
list_hindi_mfcc = []
num_hindi_sequence = int(np.floor(len(hindi_mfcc)/sequence_length))
for i in range(num_hindi_sequence):
list_hindi_mfcc.append(hindi_mfcc[sequence_length*i:sequence_length*(i+1)])
list_hindi_mfcc = np.array(list_hindi_mfcc)
hindi_labels = np.full((num_hindi_sequence, 1000, 3), np.array([0, 1, 0]))
list_mandarin_mfcc = []
num_mandarin_sequence = int(np.floor(len(mandarin_mfcc)/sequence_length))
for i in range(num_mandarin_sequence):
list_mandarin_mfcc.append(mandarin_mfcc[sequence_length*i:sequence_length*(i+1)])
list_mandarin_mfcc = np.array(list_mandarin_mfcc)
mandarin_labels = np.full((num_mandarin_sequence, 1000, 3), np.array([0, 0, 1]))
del english_mfcc
del hindi_mfcc
del mandarin_mfcc
total_sequence_length = num_english_sequence + num_hindi_sequence + num_mandarin_sequence
Y_train = np.vstack((english_labels, hindi_labels))
Y_train = np.vstack((Y_train, mandarin_labels))
X_train = np.vstack((list_english_mfcc, list_hindi_mfcc))
X_train = np.vstack((X_train, list_mandarin_mfcc))
del list_english_mfcc
del list_hindi_mfcc
del list_mandarin_mfcc
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size=0.2)
with h5py.File("mfcc_dataset.hdf5", 'w') as hf:
hf.create_dataset('X_train', data=X_train)
hf.create_dataset('Y_train', data=Y_train)
hf.create_dataset('X_val', data=X_val)
hf.create_dataset('Y_val', data=Y_val)
# ---------------------------------------------------------------
# --------------------------BLOCK 2-------------------------------------
# Load MFCC Dataset created by the code in the previous steps
with h5py.File("mfcc_dataset.hdf5", 'r') as hf:
X_train = hf['X_train'][:]
Y_train = hf['Y_train'][:]
X_val = hf['X_val'][:]
Y_val = hf['Y_val'][:]
# ---------------------------------------------------------------
# ---------------------------BLOCK 3------------------------------------
# Setting up the model for training
DROPOUT = 0.3
RECURRENT_DROP_OUT = 0.2
optimizer = optimizers.Adam(decay=1e-4)
main_input = Input(shape=(sequence_length, 64), name='main_input')
# ### main_input = Input(shape=(None, 64), name='main_input')
# ### pred_gru = GRU(4, return_sequences=True, name='pred_gru')(main_input)
# ### rnn_output = Dense(3, activation='softmax', name='rnn_output')(pred_gru)
layer1 = CuDNNLSTM(64, return_sequences=True, name='layer1')(main_input)
layer2 = CuDNNLSTM(32, return_sequences=True, name='layer2')(layer1)
layer3 = Dense(100, activation='tanh', name='layer3')(layer2)
rnn_output = Dense(3, activation='softmax', name='rnn_output')(layer3)
model = Model(inputs=main_input, outputs=rnn_output)
print('\nCompiling model...')
model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['acc'])
model.summary()
history = model.fit(X_train, Y_train, batch_size=32, epochs=75, validation_data=(X_val, Y_val), shuffle=True, verbose=1)
model.save('sld.hdf5')
# ---------------------------------------------------------------
# --------------------------BLOCK 4-------------------------------------
# Inference Mode Setup
streaming_input = Input(name='streaming_input', batch_shape=(1, 1, 64))
pred_layer1 = CuDNNLSTM(64, return_sequences=True, name='layer1', stateful=True)(streaming_input)
pred_layer2 = CuDNNLSTM(32, return_sequences=True, name='layer2')(pred_layer1)
pred_layer3 = Dense(100, activation='tanh', name='layer3')(pred_layer2)
pred_output = Dense(3, activation='softmax', name='rnn_output')(pred_layer3)
streaming_model = Model(inputs=streaming_input, outputs=pred_output)
streaming_model.load_weights('sld.hdf5')
# streaming_model.summary()
# ---------------------------------------------------------------
# ---------------------------BLOCK 5------------------------------------
# Language Prediction for a random sequence from the validation data set
random_val_sample = np.random.randint(0, X_val.shape[0])
random_sequence_num = np.random.randint(0, len(X_val[random_val_sample]))
test_single = X_val[random_val_sample][random_sequence_num].reshape(1, 1, 64)
val_label = Y_val[random_val_sample][random_sequence_num]
true_label = language_name(np.argmax(val_label))
print("***********************")
print("True label is ", true_label)
single_test_pred_prob = streaming_model.predict(test_single)
pred_label = language_name(np.argmax(single_test_pred_prob))
print("Predicted label is ", pred_label)
print("***********************")
# ---------------------------------------------------------------
# ---------------------------BLOCK 6------------------------------------
## COMMENT/UNCOMMENT BELOW
# Prediction for all sequences in the validation set - Takes very long to run
print("Predicting labels for all sequences - (Will take a lot of time)")
list_pred_labels = []
for i in range(X_val.shape[0]):
for j in range(X_val.shape[1]):
test = X_val[i][j].reshape(1, 1, 64)
seq_predictions_prob = streaming_model.predict(test)
predicted_language_index = np.argmax(seq_predictions_prob)
list_pred_labels.append(predicted_language_index)
pred_english = list_pred_labels.count(0)
pred_hindi = list_pred_labels.count(1)
pred_mandarin = list_pred_labels.count(2)
print("Number of English labels = ", pred_english)
print("Number of Hindi labels = ", pred_hindi)
print("Number of Mandarin labels = ", pred_mandarin)
# ---------------------------------------------------------------
```
```Traceback (most recent call last):
File "C:\Users\SKYLAND-2\Documents\nipunmanral SLR\language_identification.py", line 79, in <module>
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size=0.2)
File "C:\Users\SKYLAND-2\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\model_selection\_split.py", line 2417, in train_test_split
arrays = indexable(*arrays)
File "C:\Users\SKYLAND-2\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\utils\validation.py", line 378, in indexable
check_consistent_length(*result)
File "C:\Users\SKYLAND-2\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\utils\validation.py", line 332, in check_consistent_length
raise ValueError(
ValueError: Found input variables with inconsistent numbers of samples: [3, 0]```
hi, i am trying to run the code which belong to nipunmanral spoken language identification and i received this error. this is my first time learning machine learning, i am trying to learn spoken language identification which classify what type of language from an audio. i hope someone can share some tutorial or fix the error.
I am coming from medical background and a newbie in this machine learning field. I am trying to train my U-Net model using keras and tensorflow for image segmentation. However, my loss value is all NaN and the prediction is all black.
I would like to check the U-Net layer by layer but I don't know how to feed the data and from where to start. What I meant by checking for each layer is that I want to feed my images to first layer for example and see the output from the first layer and then moving on to the second layer and until to the last layer. Just want to see how the output is produced for each layer and to check from where the nan value is started. Really appreciate for your help.
These are my codes.
import os
import matplotlib.pyplot as plt
import tensorflow as tf
from keras_preprocessing.image
import ImageDataGenerator
from tensorflow import keras
#Constants
SEED = 42
BATCH_SIZE_TRAIN = 16
BATCH_SIZE_TEST = 16
IMAGE_HEIGHT = 512
IMAGE_WIDTH = 512
IMG_SIZE = (IMAGE_HEIGHT, IMAGE_WIDTH)
data_dir = 'data'
data_dir_train = os.path.join(data_dir, 'training')
data_dir_train_image = os.path.join(data_dir_train, 'img')
data_dir_train_mask = os.path.join(data_dir_train, 'mask')
data_dir_test = os.path.join(data_dir, 'test')
data_dir_test_image = os.path.join(data_dir_test, 'img')
data_dir_test_mask = os.path.join(data_dir_test, 'mask')
NUM_TRAIN = 1413
NUM_TEST = 210
NUM_OF_EPOCHS = 10
def create_segmentation_generator_train(img_path, mask_path, BATCH_SIZE):
data_gen_args = dict(rescale=1./255)
img_datagen = ImageDataGenerator(**data_gen_args)
mask_datagen = ImageDataGenerator(*data_gen_args)
img_generator = img_datagen.flow_from_directory(img_path, target_size=IMG_SIZE, class_mode=None, color_mode='grayscale', batch_size=BATCH_SIZE, seed=SEED)
mask_generator = mask_datagen.flow_from_directory(mask_path, target_size=IMG_SIZE, class_mode=None, color_mode='grayscale', batch_size=BATCH_SIZE, seed=SEED)
return zip(img_generator, mask_generator)
def create_segmentation_generator_test(img_path, mask_path, BATCH_SIZE):
data_gen_args = dict(rescale=1./255)
img_datagen = ImageDataGenerator(**data_gen_args)
mask_datagen = ImageDataGenerator(*data_gen_args)
img_generator = img_datagen.flow_from_directory(img_path, target_size=IMG_SIZE, class_mode=None, color_mode='grayscale', batch_size=BATCH_SIZE, seed=SEED)
mask_generator = mask_datagen.flow_from_directory(mask_path, target_size=IMG_SIZE, class_mode=None, color_mode='grayscale', batch_size=BATCH_SIZE, seed=SEED)
return zip(img_generator, mask_generator)
def display(display_list):
plt.figure(figsize=(15,15))
title = ['Input Image', 'True Mask', 'Predicted Mask']
for i in range(len(display_list)):
plt.subplot(1, len(display_list), i+1)
plt.title(title[i])
plt.imshow(tf.keras.preprocessing.image.array_to_img(display_list[i]), cmap='gray')
plt.show()
def show_dataset(datagen, num=1):
for i in range(0,num):
image,mask = next(datagen)
display([image[0], mask[0]])
def unet(n_levels, initial_features=32, n_blocks=2, kernel_size=3, pooling_size=2, in_channels=1, out_channels=1):
#n_blocks = how many conv in each level
inputs = keras.layers.Input(shape=(IMAGE_HEIGHT, IMAGE_WIDTH, in_channels))
x = inputs
convpars = dict(kernel_size=kernel_size, activation='relu', padding='same')
#downstream
skips = {}
for level in range(n_levels):
for _ in range (n_blocks):
x = keras.layers.Conv2D(initial_features * 2 ** level, **convpars)(x)
if level < n_levels - 1:
skips[level] = x
x = keras.layers.MaxPool2D(pooling_size)(x)
#upstream
for level in reversed(range(n_levels-1)):
x = keras.layers.Conv2DTranspose(initial_features * 2 ** level, strides=pooling_size, **convpars)(x)
x = keras.layers.Concatenate()([x, skips[level]])
for _ in range (n_blocks):
x = keras.layers.Conv2D(initial_features * 2 ** level, **convpars)(x)
#output
activation = 'sigmoid' if out_channels == 1 else 'softmax'
x = keras.layers.Conv2D(out_channels, kernel_size=1, activation='sigmoid', padding='same')(x)
return keras.Model(inputs=[inputs], outputs=[x], name=f'UNET-L{n_levels}-F{initial_features}')
EPOCH_STEP_TRAIN = NUM_TRAIN // BATCH_SIZE_TRAIN
EPOCH_STEP_TEST = NUM_TEST // BATCH_SIZE_TRAIN
model = unet(4)
model.compile(optimizer="adam", loss='binary_crossentropy', metrics=['accuracy'])
model.fit_generator(generator=train_generator, steps_per_epoch=EPOCH_STEP_TRAIN, validation_data=test_generator, validation_steps=EPOCH_STEP_TEST, epochs=NUM_OF_EPOCHS)
def show_prediction(datagen, num=1):
for i in range(0,num):
image,mask = next(datagen)
pred_mask = model.predict(image)[0] > 0.5
display([image[0], mask[0], pred_mask])
show_prediction(test_generator, 2)
To investigate your model layer-by-layer please see example how to show summary of the model and also how to save the model:
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
#luodaan input
inputs=keras.Input(shape=(1,))
#luodaan kerros
dense=layers.Dense(64,activation="relu")
x=dense(inputs)
x=layers.Dense(64,activation="relu")(x)
outputs=layers.Dense(10)(x)
#Koostetaa
model=keras.Model(inputs=inputs,outputs=outputs,name="Spesiaali")
#Tarkastellaan
model.summary()
#Tallennellaan
model.save(".\model_to_be_investigated_by_someone_else_to_help_you")
...this makes it possible for you to see the whole model structure for "debugging your AI". If you do not find the solution itself, then add the last row of example to your own code, and then put the resulting folder e.g. to github and ask someone other to see the structure of your model to help you in solving the problem.
The blue drawing illustrates the output of command model.summary() and the red line illustrates the output shape of the first dense layer.
I'm doing this kaggle contest where i have to classify this x-ray in 3 category bacteria,virus or normal.
I don't get why it keep me give the same error.
Images are rgb, and output shape is (none,3) so I really don't get where the thing with shape (none,1) is.
Can someone help me?
Here is my code:
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
TRAIN_DIR = 'D:/tf/archiveBilanciato/chest_xray/train/PNEUMONIA'
TEST_DIR = 'D:/tf/archiveBilanciato/chest_xray/test'
IMG_SIZE = 224 #224 รจ quella migliore
IMAGE_SIZE = (IMG_SIZE, IMG_SIZE)
BATCH_SIZE = 32
LR = 1e-3
import os
nt = 0
for folder_name in ("bacteria", "normal","virus"):
folder_path = os.path.join("D:/tf/NeoArchiveBilanciato/chest_xray", folder_name)
for fname in os.listdir(folder_path):
fpath = os.path.join(folder_path, fname)
nt += 1
print("Totale immagini di questa categoria: %d" % nt)
nt = 0
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
"D:/tf/NeoArchiveBilanciato/chest_xray",
validation_split=0.2,
subset="training",
seed=1337,
color_mode='rgb',
image_size=IMAGE_SIZE,
batch_size=BATCH_SIZE,
)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
"D:/tf/NeoArchiveBilanciato/chest_xray",
validation_split=0.2,
subset="validation",
seed=1337,
color_mode='rgb',
image_size=IMAGE_SIZE,
batch_size=BATCH_SIZE,
)
import tensorflow as tf
import numpy as np
from tensorflow import keras
from tensorflow.keras.applications import DenseNet121
from keras.layers import GlobalAveragePooling2D,Dense
def pre_model():
base_model = tf.keras.applications.DenseNet121(
weights='imagenet', include_top=False, input_shape=(224, 224, 3))
x = base_model.output
x = GlobalAveragePooling2D()(x)
predictions = Dense(14, activation="softmax")(x)
pre_model = keras.Model(inputs=base_model.input, outputs=predictions)
return pre_model
base_model = pre_model()
base_model.load_weights("D:/tf/nih_pretrained_chest_model.h5")
print("base_model")
print(base_model.summary())
base_model.trainable = False
mio_classificatore = Dense(3, activation='softmax')(base_model.layers[-2].output)
print("mio_classificatore.get_shape()")
print(mio_classificatore.get_shape())
nuovo_model = keras.Model(inputs=base_model.input, outputs=mio_classificatore)
print("nuovo_model")
print(nuovo_model.summary())
train = train_ds.prefetch(buffer_size=32)
val = val_ds.prefetch(buffer_size=32)
callbacks = [
keras.callbacks.ModelCheckpoint("save_at_{epoch}.h5"),
]
nuovo_model.compile(optimizer=keras.optimizers.Adam(LR),
loss=keras.losses.CategoricalCrossentropy(),
metrics=[keras.metrics.Accuracy()])
nuovo_model.fit(train,batch_size=32, epochs=14,callbacks=callbacks, validation_data=val)
The error occur at model.fit(...) where i get this message:
ValueError: Shapes (None, 1) and (None, 3) are incompatible
Unlike the DataImageGenerator from keras the image_dataset_from_directory defaults to integer labels.
If you want to use the categorical_crossentropy loss function, you need to define label_mode='categorical' in image_dataset_from_directory() to get One-Hot encoded labels.
See the documentation here.
I'm trying to do Saliency Map Method using cleverhans.
My model needs to be keras sequential so for that reason I've searched and found cleverhans.utils_keras, Sequential uses KerasModelWrapper. But for some reason I still get it should be cleverhans model. Here's the stacktrace
TypeError Traceback (most recent call last)
in
2 # https://github.com/tensorflow/cleverhans/blob/master/cleverhans/utils_keras.py
3
----> 4 jsma = SaliencyMapMethod(model, sess=sess)
5 jsma_params = {'theta': 10.0, 'gamma': 0.15,
6 'clip_min': 0., 'clip_max': 1.,
c:\users\jeredriq\appdata\local\programs\python\python35\lib\site-packages\cleverhans\attacks__init__.py in init(self, model, sess, dtypestr, **kwargs)
911 """
912
--> 913 super(SaliencyMapMethod, self).init(model, sess, dtypestr, **kwargs)
914
915 self.feedable_kwargs = ('y_target',)
c:\users\jeredriq\appdata\local\programs\python\python35\lib\site-packages\cleverhans\attacks__init__.py in init(self, model, sess, dtypestr, **kwargs)
55
56 if not isinstance(model, Model):
---> 57 raise TypeError("The model argument should be an instance of"
58 " the cleverhans.model.Model class.")
59
TypeError: The model argument should be an instance of the cleverhans.model.Model class.
And here's my code
import numpy as np
from keras import backend
import tensorflow as tf
from keras.callbacks import ModelCheckpoint
from matplotlib import gridspec
from matplotlib import pyplot as plt
from sklearn.metrics import confusion_matrix, classification_report
from keras.datasets import mnist
from keras.layers import Dense, Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.utils import np_utils
from cleverhans.attacks import FastGradientMethod
from cleverhans.attacks import BasicIterativeMethod
from cleverhans.attacks import SaliencyMapMethod
from cleverhans.attacks import DeepFool
from cleverhans.utils_keras import Sequential
sess = backend.get_session()
x = tf.placeholder(tf.float32, shape=(None, 28, 28, 1))
y = tf.placeholder(tf.float32, shape=(None, 10))
# Managing Mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)
X_test = X_test.reshape(X_test.shape[0], 28, 28, 1)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train/=255
X_test/=255
y_train_cat = np_utils.to_categorical(y_train)
y_test_cat = np_utils.to_categorical(y_test)
num_classes = y_test_cat.shape[1]
### Defining Model ###
model = Sequential() # <----- I use Sequential from CleverHans
model.add(Conv2D(32, (5, 5), input_shape=(28,28,1), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()
history = model.fit(X_train, y_train_cat, epochs=10, batch_size=1024, verbose=1, validation_split=0.7)
### And the problem part ###
jsma = SaliencyMapMethod(model, sess=sess) # <---- Where I get the exception
jsma_params = {'theta': 10.0, 'gamma': 0.15,
'clip_min': 0., 'clip_max': 1.,
'y_target': None}
sample_size = 20
one_hot_target = np.zeros((sample_size, 10), dtype=np.float32)
one_hot_target[:, 1] = 1
jsma_params['y_target'] = one_hot_target
X_test_small = X_test[0:sample_size,:]
y_test_small = y_test[0:sample_size]
adv_x = jsma.generate_np(X_test_small, **jsma_params)
I've the same question on github too.
The Sequential defined in cleverhans.utils_keras is still keras' Sequential model. What is needed is cleverhans.model.Model. A keras model can be wrapped to provide this behaviour by using the KerasModelWrapper class.
Replace
jsma = SaliencyMapMethod(model, sess=sess)
with
jsma = SaliencyMapMethod(KerasModelWrapper(model), sess=sess)