I am using InceptionResNetV2 for image classification & using repective weight. But get error :
ValueError: You are trying to load a weight file containing 449 layers into a model with 448 layers.
img_ht = 96
img_wid = 96
img_chnl = 3
import tensorflow as tf
from tensorflow import keras
from keras_preprocessing.image import ImageDataGenerator
train_generator = train_datagen.flow_from_directory(
directory = "../input/cassava-disease/train/train/",
subset="training",
batch_size = 49,
seed=42,
shuffle=False,
class_mode="categorical",
target_size=(img_ht, img_wid))
valid_generator = train_datagen.flow_from_directory(
directory = "../input/cassava-disease/train/train/",
subset="validation",
batch_size=49,
seed=42,
shuffle=False,
class_mode="categorical",
target_size = (img_ht, img_wid))
from keras.applications import InceptionResNetV2 as InceptionResNetV2
base_model = keras.applications.InceptionResNetV2(input_shape=(img_ht, img_wid, 3),
include_top = False,
weights = "../input/inception/inception_resnet_v2_weights_tf_dim_ordering_tf_kernels.h5")
base_model.trainable = False
print(base_model.summary())
Got the answer. It's because of line --> include_top = False.
Quite new to python & Machine Learning
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.].
I am trying to train a DenseNet121 model on chest X-ray images using tensorflow.keras, and using ImageDataGenerator for augmentation. I have directories of files containing symlinks to the images that I believe is set up in the correct format for ImageDataGenerator:
Train
Normal
Abnormal
Val
Normal
Abnormal
However, when I call model.fit(), it throws FileNotFoundError: [Errno 2] No such file or directory: '.\\Train\\Normal\\00017275_014.png' which is a symlink file. .flow_from_directory(follow_links = True) did not solve the problem. Also, calling os.islink() with that path returns True.
In addition: calling imagedatagenerator returns:
Found 84090 images belonging to 2 classes. Found 28030 images belonging to 2 classes.
Any suggestions? Code below:
from tensorflow.keras.applications.densenet import preprocess_input
from tensorflow.keras import Model,layers
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import Adam, SGD
from tensorflow.keras.metrics import binary_accuracy
from tensorflow.keras.losses import binary_crossentropy
batch_size = 64
train_datagen = ImageDataGenerator(
preprocessing_function = preprocess_input,
brightness_range = [0.75, 1.25],
horizontal_flip=True,
)
train_generator = train_datagen.flow_from_directory(
directory = '.\\Train',
color_mode = 'rgb',
classes = ['Normal', 'Abnormal'],
class_mode = 'binary',
batch_size = batch_size,
target_size = (224,224),
follow_links=True,
)
val_datagen = ImageDataGenerator(
preprocessing_function = preprocess_input,
)
val_generator = val_datagen.flow_from_directory(
directory = '.\\Val',
color_mode = 'rgb',
class_mode = 'binary',
classes = ['Normal', 'Abnormal'],
batch_size = batch_size,
target_size = (224,224),
follow_links = True,
)
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
model_name = "Imagenet DenseNet121 on NIH full dataset 375 locked brightness flip.h5"
callback_checkpoint = [
EarlyStopping(monitor = 'val_loss', patience = 10, verbose = 1),
ModelCheckpoint(model_name,
verbose = 1,
monitor = 'val_loss',
save_best_only = True,
)
]
model.compile(
optimizer = Adam(),
#optimizer = SGD(learning_rate = 0.001, momentum = 0.9, decay = 0.0001),
loss = 'binary_crossentropy',
metrics = ['binary_accuracy'],
)
history = model.fit(
train_generator,
steps_per_epoch=1250,
epochs=50,
validation_data=val_generator,
validation_steps=437,
callbacks = [callback_checkpoint],
)
`os.path.islink((os.path.join(os.getcwd(),
"Train",
"Normal",
"00017275_014.png")))
True`
At least for pathlib.Path the combined notation with dot and double backslash is not valid. I guess this is the problem here also. Try using forward slashes. Instead of directory = ".\\Val" try
directory = "./Val"
or simply
directory = "Val"
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 am trying to experiment on ISIC 2019 data as a newbie. Firstly, I downloaded the training data and divided the data into 3 parts as train, test, and validation data, and every dataset folder contains 2 subfolders which are benign and malignant. In short, I just moved all the categories into benign folders except the melenoma category and melanoma images are inside malignant folders. After the division, I get imbalanced data. In the training dataset for benign data, I get 16596 images and for malignant data, I get 3629 images. I tried to train my data and I couldn't get a good result for malignant and my precision value was about 0.18 for malignant. I used ResNet50 to train my model and I would like to ask how can I train my model without data augmentation and oversampling? I am also trying decayed learning metrics at the moment and it seems it won't give a good result too.
import os
import tensorflow as tf
import math
import numpy as np
import matplotlib.pyplot as plt
from tensorflow import keras
from keras.applications.resnet50 import ResNet50, preprocess_input
from keras.layers import Dense, GlobalMaxPooling2D
from keras.models import Model
from keras.optimizers import Adam
from sklearn.metrics import roc_curve
from tensorflow.keras.preprocessing.image import ImageDataGenerator
train_examples = 20225
test_examples = 2551
validation_examples = 2555
img_height = img_width = 224
channel = 3
batch_size = 32
base_model = ResNet50(weights = 'imagenet' , include_top = False, input_shape = (img_height, img_width, channel))
x = base_model.output
x = GlobalMaxPooling2D()(x)
x = Dense(1, activation= 'sigmoid')(x)
model = Model(
inputs = base_model.input,
outputs = x)
model.summary()
train_datagen = ImageDataGenerator(
rotation_range = 20,
width_shift_range=0.10,
height_shift_range=0.10,
zoom_range = 0.10,
horizontal_flip = True,
preprocessing_function = preprocess_input,
fill_mode='nearest'
)
validation_datagen = ImageDataGenerator(
preprocessing_function = preprocess_input,
)
test_datagen = ImageDataGenerator(
preprocessing_function = preprocess_input,
)
train_gen = train_datagen.flow_from_directory(
"dataset/train/",
target_size = (img_height, img_width),
batch_size = batch_size,
color_mode = "rgb",
class_mode = "binary",
shuffle = True,
seed = 123,
)
validation_gen = validation_datagen.flow_from_directory(
"dataset/validation/",
target_size = (img_height, img_width),
batch_size = batch_size,
color_mode = "rgb",
class_mode = "binary",
shuffle = True,
seed = 123,
)
test_gen = test_datagen.flow_from_directory(
"dataset/test/",
target_size =(img_height, img_width),
batch_size = batch_size,
color_mode = "rgb",
class_mode = "binary",
shuffle = True,
seed = 123,
)
METRICS = [
keras.metrics.Precision(name = "precision"),
keras.metrics.Recall(name = "recall"),
keras.metrics.AUC(name = "auc"),
]
model.compile(
optimizer = Adam(lr = 3e-4),
loss = [keras.losses.BinaryCrossentropy(from_logits = False)],
metrics = METRICS,
)
history = model.fit(train_gen,
epochs=50,
verbose=1,
validation_data=validation_gen,
callbacks=[keras.callbacks.ModelCheckpoint("isic_binary_model")],
)
I am performing binary classification of data. I am using predict_generator to obtain the classification results. The input to the predict_generator are 44 examples, 22 positive, 22 negative. The following is the output obtained:
[9.98187363e-01 1.81267178e-03]
[5.02341951e-04 9.99497652e-01]
[8.41189444e-01 1.58810586e-01]
[7.26610771e-04 9.99273360e-01]
[9.96826649e-01 3.17337317e-03]
[8.83008718e-01 1.16991334e-01]
[3.84130690e-04 9.99615788e-01]
[8.65039527e-01 1.34960532e-01]
[1.78014021e-03 9.98219788e-01]
[9.96107757e-01 3.89222591e-03]
[6.16264821e-04 9.99383688e-01]
[2.98170745e-03 9.97018337e-01]
[9.92357790e-01 7.64221745e-03]
[9.93237853e-01 6.76209433e-03]
[9.98248339e-01 1.75163767e-03]
[1.17816392e-03 9.98821795e-01]
[9.84322488e-01 1.56775210e-02]
[3.11790430e-03 9.96882081e-01]
[4.62388212e-04 9.99537587e-01]
[1.42699364e-03 9.98572946e-01]
[9.43281949e-01 5.67180961e-02]
[9.98008907e-01 1.99115812e-03]
[4.12312744e-04 9.99587715e-01]
[9.29474115e-01 7.05258474e-02]
[3.37766513e-04 9.99662280e-01]
[1.75693433e-03 9.98243093e-01]
[9.92154586e-04 9.99007881e-01]
[1.87152205e-03 9.98128474e-01]
[9.20654461e-02 9.07934546e-01]
[9.95722532e-01 4.27750358e-03]
[9.96877313e-01 3.12273414e-03]
[9.87601459e-01 1.23985587e-02]
[1.11398198e-01 8.88601840e-01]
[1.48968585e-02 9.85103130e-01]
[6.73048152e-03 9.93269503e-01]
[1.65761902e-03 9.98342395e-01]
[9.94634032e-01 5.36595425e-03]
[5.00697970e-01 4.99302000e-01]
[1.65578525e-03 9.98344183e-01]
[9.68859911e-01 3.11401486e-02]
CODE:
from keras.applications import Xception
from keras.models import Model
from keras.layers import Dense, Input, Dropout
from keras.optimizers import Nadam
from keras.callbacks import EarlyStopping, ModelCheckpoint
from keras.preprocessing.image import ImageDataGenerator
img_height = 299
img_width = 299
no_of_frames = 15
channels = 3
no_of_epochs = 50
batch_size_value = 60
cnn_base = Xception(input_shape=(img_width, img_height, channels),
weights="imagenet", include_top=False, pooling='avg')
cnn_base.trainable = False
hidden_layer_1 = Dense(activation="relu", units=1024)(cnn_base.output)
drop_layer=Dropout(0.2)(hidden_layer_1)
hidden_layer_2 = Dense(activation="relu", units=512)(drop_layer)
outputs = Dense(2, activation="softmax")(hidden_layer_2)
model = Model(cnn_base.input, outputs)
nadam_optimizer = Nadam(lr=0.0001, beta_1=0.9, beta_2=0.999,
epsilon=1e-08, schedule_decay=0.004)
model.compile(loss="categorical_crossentropy",
optimizer=nadam_optimizer, metrics=["accuracy"])
# for data augmentation
train_datagen = ImageDataGenerator( zoom_range=.1, rotation_range=8,
width_shift_range=.2, height_shift_range=.2)
train_generator = train_datagen.flow_from_directory(
'/home/Train', # this is the target directory
target_size=(img_width, img_height),
batch_size=batch_size_value,
class_mode="categorical")
validation_generator = ImageDataGenerator().flow_from_directory(
'/home/Val',
target_size=(img_width, img_height),
batch_size=batch_size_value,
class_mode="categorical")
history = model.fit_generator(
train_generator,
validation_data=validation_generator,
verbose=1,
epochs=no_of_epochs,
steps_per_epoch=17,
validation_steps=7)
Test_generator = ImageDataGenerator().flow_from_directory(
'/home/Test',
target_size=(img_width, img_height),
batch_size=batch_size_value,
class_mode="categorical")
Prob_val = model.predict_generator(test_set)
print((Prob_val))
I assume they are probabilities, but the column contains only 40 entries. How do they correspond with the 44 inputs example ?