Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

keras imagenet

from tensorflow.keras.applications.inception_v3 import InceptionV3
from tensorflow.keras.preprocessing import image
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D

# create the base pre-trained model
base_model = InceptionV3(weights='imagenet', include_top=False)

# add a global spatial average pooling layer
x = base_model.output
x = GlobalAveragePooling2D()(x)
# let's add a fully-connected layer
x = Dense(1024, activation='relu')(x)
# and a logistic layer -- let's say we have 200 classes
predictions = Dense(200, activation='softmax')(x)

# this is the model we will train
model = Model(inputs=base_model.input, outputs=predictions)

# first: train only the top layers (which were randomly initialized)
# i.e. freeze all convolutional InceptionV3 layers
for layer in base_model.layers:
    layer.trainable = False

# compile the model (should be done *after* setting layers to non-trainable)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')

# train the model on the new data for a few epochs
model.fit(...)

# at this point, the top layers are well trained and we can start fine-tuning
# convolutional layers from inception V3. We will freeze the bottom N layers
# and train the remaining top layers.

# let's visualize layer names and layer indices to see how many layers
# we should freeze:
for i, layer in enumerate(base_model.layers):
   print(i, layer.name)

# we chose to train the top 2 inception blocks, i.e. we will freeze
# the first 249 layers and unfreeze the rest:
for layer in model.layers[:249]:
   layer.trainable = False
for layer in model.layers[249:]:
   layer.trainable = True

# we need to recompile the model for these modifications to take effect
# we use SGD with a low learning rate
from tensorflow.keras.optimizers import SGD
model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy')

# we train our model again (this time fine-tuning the top 2 inception blocks
# alongside the top Dense layers
model.fit(...)
Comment

PREVIOUS NEXT
Code Example
Python :: Errors that you will get during date object in python datetime 
Python :: Convert Time object to String in python 
Python :: Delete file to trash 
Python :: how to make a timer in pyothn 
Python :: Top n rows of each group 
Python :: Python send sms curl 
Python :: Customize tick spacing 
Python :: bs4 check element type 
Python :: Python Windows Toggle Caps_Lock 
Python :: string letters only 
Python :: different accuracy score for knn 
Python :: BMI CALCULATOR CODE IN PYTHON 
Python :: how to open local software using python 
Python :: Extracting the cluster labels from a dendrogram 
Python :: rich import in python 
Python :: HIDING AND ENCRYPTING PASSWORDS IN PYTHON USING ADVPASS 
Python :: How to use a function output as an input of another function in Python 
Python :: pd.generate_date 
Python :: what will be the output of the following python code? x = 123 for i in x: print(i) 
Python :: how to import modules from upper or previous dir in py 
Python :: 1046 - Game Time 
Python :: python csv string to array 
Python :: captcha.image install in python 
Python :: python sum whole matrix comand 
Python :: infinty in python 
Python :: python get text between two comma 
Python :: créer un dict python avec une liste 
Python :: python run scp command 
Python :: flask-restx custom ui 
Python :: Applies a function to all elements of this RDD. 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =