# Force TensorFlow to use single thread. Multiple threads are a potential source of non-reproducible results. session_conf = tf.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1) from keras import backend as K # The below tf.set_random_seed() will make random number generation in the TensorFlow backend have a well-defined initial state. tf.set_random_seed(1234) sess = tf.Session(graph=tf.get_default_graph(), config=session_conf) K.set_session(sess) num_classes = 17 image_size = 512 my_new_model = Sequential() my_new_model.add(ResNet50(include_top=False, pooling='avg', weights='imagenet')) my_new_model.add(Dense(num_classes, activation='softmax')) # Say not to train first layer (ResNet) model. It is already trained my_new_model.layers[0].trainable = False my_new_model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy']) data_generator_train = ImageDataGenerator(rescale=1. / 127.5) data_generator_test = ImageDataGenerator() train_generator = data_generator_train.flow_from_directory( '../TFM/Dataset_Resize_Split_Train', target_size=(image_size, image_size),
return preprocess_input(img_array) # # 3) Modeling # 1. Create a Resnet50 model and save it as `my_model`. # 2. Apply the `read_and_prep_images` function to `image_paths` and save the result as `image_data`. # 3. Use `my_model` to predict the contents of `image_data`. Store the results in `my_preds`. # 4. Apply the `decode_predictions` function to `my_preds`. We could focus on just the top prediction for each image with the argument `top=1`. # # You can review the instructional page to remind yourself how to do this. # In[8]: # Code Here from tensorflow.python.keras.applications import ResNet50 my_model = ResNet50( weights='../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels.h5') image_data = read_and_prep_images(image_paths) my_preds = my_model.predict(image_data) import sys # Add directory holding utility functions to path to allow importing sys.path.append( '/kaggle/input/python-utility-code-for-deep-learning-exercises/utils') from decode_predictions import decode_predictions from IPython.display import Image, display most_likely_labels = decode_predictions( my_preds, top=3, class_list_path='../input/resnet50/imagenet_class_index.json')
__date__ = '2018/12/20' ''' from tensorflow.python.keras.models import Sequential from tensorflow.python.keras.applications import ResNet50 from tensorflow.python.keras.layers import Dense from tensorflow.python.keras.applications.resnet50 import preprocess_input from tensorflow.python.keras.preprocessing.image import ImageDataGenerator # 1. Specify and Compile num_classes = 2 my_new_model = Sequential() my_new_model.add( ResNet50( include_top=False, pooling='avg', weights= '../datasets/ResNet-50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5' )) my_new_model.add(Dense(num_classes, activation='softmax')) # first layer: ResNet50 (already trained) my_new_model.layers[0].trainable = False my_new_model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy']) # 2. Fit the model image_size = 224 data_generator = ImageDataGenerator(preprocess_input) train_generator = data_generator.flow_from_directory( directory='../datasets/dogs/train', target_size=(image_size, image_size),
from tensorflow.python.keras.applications import ResNet50 from tensorflow.python.keras.models import Sequential from tensorflow.python.keras.layers import Dense, Flatten, GlobalAveragePooling2D # num_classes is the number of categories your model chooses between for each prediction num_classes = 2 resnet_weights_path = 'input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5' #resnet_weights_path = 'resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5' my_new_model = Sequential() my_new_model.add( ResNet50(include_top=False, pooling='avg', weights=resnet_weights_path)) my_new_model.add(Dense(num_classes, activation='softmax')) # The value below is either True or False. If you choose the wrong answer, your modeling results # won't be very good. Recall whether the first layer should be trained/changed or not. my_new_model.layers[0].trainable = False # We are calling the compile command for some python object. # Which python object is being compiled? Fill in the answer so the compile command works. my_new_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) from tensorflow.python.keras.applications.resnet50 import preprocess_input from tensorflow.python.keras.preprocessing.image import ImageDataGenerator image_size = 224 data_generator = ImageDataGenerator() train_generator = data_generator.flow_from_directory( directory='input/dogs-gone-sideways/images/train',
import cv2 as cv import os #from keras.applications import ResNet50 #from keras.callbacks import ModelCheckpoint from tensorflow.python.keras.optimizers import SGD from tensorflow.python.keras.applications import ResNet50 from tensorflow.python.keras.models import Sequential from tensorflow.python.keras.layers import Dense from tensorflow.python.keras.preprocessing import image as image_utils num_classes = 2 classifier = Sequential() classifier.add(ResNet50( include_top=False, pooling='avg', )) classifier.add(Dense(num_classes, activation='softmax')) # Say yes to train first layer (ResNet) model. classifier.layers[0].trainable = False #Set Optimizer opt = SGD(lr=1e-4, momentum=0.9) #Compile Model classifier.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy']) classifier.summary()
NUM_EPOCHS = 10 EARLY_STOP_PATIENCE = 3 STEPS_PER_EPOCH_TRAINING = 10 STEPS_PER_EPOCH_VALIDATION = 10 BATCH_SIZE_TRAINING = 10 BATCH_SIZE_VALIDATION = 10 resnet_weights_path = '../models/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5' model = Sequential() model.add( ResNet50(include_top=False, pooling=RESNET50_POOLING_AVERAGE, weights=resnet_weights_path)) model.add(Dense(NUM_CLASSES, activation=DENSE_LAYER_ACTIVATION)) model.layers[0].trainable = False sgd = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) model.compile(optimizer=sgd, loss=OBJECTIVE_FUNCTION, metrics=LOSS_METRICS) cb_early_stopper = EarlyStopping(monitor='val_loss', patience=EARLY_STOP_PATIENCE) cb_checkpointer = ModelCheckpoint( filepath='../models/trained_models/best2.hdf5', monitor='val_loss', save_best_only=True, mode='auto')
def create_model(): model = ResNet50(include_top=False, pooling='avg') new_model = Sequential() new_model.add(model) new_model.add(Dense(1, )) return new_model
X_mean = np.mean(X_train) X_std = np.std(X_train) X_mean_val = np.mean(X_test) X_std_val = np.std(X_test) X_train = (X_train - X_mean) / X_std X_test = (X_test - X_mean_val) / X_std_val Y_train = to_categorical(Y_train, num_classes=7) Y_test = to_categorical(Y_test, num_classes=7) model = Sequential() model.add(ResNet50(include_top=False, pooling='avg', weights=None)) model.add(Flatten()) model.add(BatchNormalization()) model.add(Dense(2048, activation='relu')) model.add(BatchNormalization()) model.add(Dense(1024, activation='relu')) model.add(BatchNormalization()) model.add(Dense(7, activation='softmax')) model.layers[0].trainable = True model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) stopping_cond = EarlyStopping(monitor='val_loss', verbose=1)
@author: User """ import time from keras.models import load_model #Specify Model from tensorflow.python.keras.applications import ResNet50 from tensorflow.python.keras.models import Sequential from tensorflow.python.keras.layers import Dense, Flatten, GlobalAveragePooling2D from keras.callbacks import ModelCheckpoint num_classes = 2 my_new_model = Sequential() my_new_model.add(ResNet50(include_top=False, pooling='avg',)) my_new_model.add(Dense(num_classes, activation='softmax')) # Say yes to train first layer (ResNet) model. my_new_model.layers[0].trainable = True #Compile Model my_new_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) my_new_model.summary() # checkpoint filepath="{epoch:03d}-acc_{acc:.5f}-valacc_{val_acc:.5f}.hdf5" checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=False, mode='max') callbacks_list = [checkpoint]
person_name = np.array(person_name) x_train, x_valid, y_train, y_valid = train_test_split(train_images, person_name, test_size=0.2, random_state=0) #print(y_train) #print(len(y_train)) #Converting to binary class y_train = keras.utils.to_categorical(y_train, 1000) y_valid = keras.utils.to_categorical(y_valid, 1000) #Creating denseNet model model = ResNet50(include_top=True, weights='imagenet', input_shape=(224, 224, 3), pooling=None, classes=1000) #model.summary() model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) model.fit(x_train, y_train, batch_size=10, epochs=1, verbose=1, validation_data=(x_valid, y_valid))
# Fill in the blanks (marked with `____`) in the code below. Then uncomment those lines and run the cell. # # In[2]: from tensorflow.python.keras.applications import ResNet50 from tensorflow.python.keras.models import Sequential from tensorflow.python.keras.layers import Dense, Flatten, GlobalAveragePooling2D # num_classes is the number of categories your model chooses between for each prediction num_classes = 2 resnet_weights_path = '../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5' my_new_model = Sequential() my_new_model.add(ResNet50(include_top=False, pooling='avg', weights=resnet_weights_path)) my_new_model.add(Dense(num_classes, activation='softmax')) # The value below is either True or False. If you choose the wrong answer, your modeling results # won't be very good. Recall whether the first layer should be trained/changed or not. my_new_model.layers[0].trainable = False # # 2) Compile the Model # # We have again provided most of the code, and left one extremely important piece blank. Fill in the blank (marked with `____`). Then uncomment that line of code and run the cell. # In[3]: # We are calling the compile command for some python object.
return correct_count / total_count image_dir = '../datasets/seefood/train' hot_dog_paths = [ os.path.join(image_dir, 'hot_dog', filename) for filename in ['1000288.jpg', '127117.jpg'] ] not_hot_dog_paths = [ os.path.join(image_dir, 'not_hot_dog', filename) for filename in ['823536.jpg', '99890.jpg'] ] image_paths = hot_dog_paths + not_hot_dog_paths my_model = ResNet50( weights= '../datasets/ResNet-50/resnet50_weights_tf_dim_ordering_tf_kernels.h5') test_imgs = read_and_prep_images(image_paths) preds = my_model.predict(test_imgs) decoded = decode_predictions(preds, top=3) for i, image_path in enumerate(image_paths): plt.imshow(plt.imread(image_path)) plt.show() print(decoded[i]) # calculate accuracy on small dataset as a test my_model_accuracy = calc_accuracy(my_model, hot_dog_paths, not_hot_dog_paths) print("Fraction correct in small test set: {}".format(my_model_accuracy)) # similarly:
image_input = Input(shape=(img_rows, img_cols, 3)) print("Tamanho X_train:", X_train.shape) print("Tamanho Y_train:", Y_train.shape) print("Tamanho X_test:", X_test.shape) print("Tamanho Y_test:", Y_test.shape) print(X_train.shape[0], 'train samples') print(X_test.shape[0], 'test samples') #cv2.imwrite("X_test1.png", X_test[1]) #Substituir o modelo pelo ResNet50/101 #Testar com as imagens do novo dataset e juntar ao treino e JAFFE, KDEF base_model = ResNet50(weights=None, include_top=False, input_shape=input_shape) x = base_model.output x = Flatten(name='flatten')(x) predictions = Dense(8, activation='softmax', name='predictions')(x) model = Model(inputs=base_model.input, outputs=predictions) for layer in model.layers[0:3]: layer.trainable = False adam = optimizers.Adam(lr=0.0001) model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=['accuracy']) model.summary() start_time = datetime.now()