IOS、Python自学心得30天 Day-22 MacOS训练模组

前言:
在寻找转换模组的方法时,也顺便写了MacOS版本的训练模组

MacOS程序码:

import os
import tensorflow as tf
import coremltools
import keras
from tensorflow import keras
from tensorflow.python.keras import backend as K
from tensorflow.python.keras.models import Model, Sequential, load_model
from tensorflow.python.keras.layers import LSTM, Flatten, Dense, Dropout
from tensorflow.python.keras.applications.resnet50 import ResNet50
from tensorflow.python.keras.optimizer_v2.adam import Adam
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator
from tensorflow.python.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau
import ssl

from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True

ssl._create_default_https_context = ssl._create_unverified_context
                     
def create_model():
    # 舍弃 ResNet50 顶层的 fully connected layers
    net = ResNet50(include_top=False, 
                weights='imagenet', 
                input_tensor=None,
                input_shape=(IMAGE_SIZE[0],IMAGE_SIZE[1],3))
    x = net.output

    x = Flatten()(x)

    # 增加 DropOut layer
    x = Dropout(0.5)(x)

    # 增加 Dense layer,以 softmax 产生个类别的机率值
    output_layer = Dense(NUM_CLASSES, activation='softmax', name='softmax')(x)

    # 设定冻结与要进行训练的网路层
    net_final = Model(inputs=net.input, outputs=output_layer)
    for layer in net_final.layers[:FREEZE_LAYERS]:
        layer.trainable = False
    for layer in net_final.layers[FREEZE_LAYERS:]:
        layer.trainable = True

    # 使用 Adam optimizer,以较低的 learning rate 进行 fine-tuning
    net_final.compile(optimizer=Adam(learning_rate=1e-5),
                    loss='categorical_crossentropy', metrics=['accuracy'])
    
    return net_final
# 资料路径
DATASET_PATH  = '/Users/pecajo/Desktop/pyFile/Demo1/'

# 影像大小
IMAGE_SIZE = (300, 300)

# 影像类别数
NUM_CLASSES = 133

# 若 GPU 记忆体不足,可调降 batch size 或冻结更多层网路
BATCH_SIZE = 8

# 冻结网路层数
FREEZE_LAYERS = 0

# Epoch 数
NUM_EPOCHS = 1

# 模型输出储存的档案
WEIGHTS_FINAL = '/Users/使用者名称/Desktop/pyFile/Demo1/model-resnet50-macOS-1.h5'

# 透过 data augmentation 产生训练与验证用的影像资料
train_datagen = ImageDataGenerator(rotation_range=40,
                                   width_shift_range=0.2,
                                   height_shift_range=0.2,
                                   shear_range=0.2,
                                   zoom_range=0.2,
                                   channel_shift_range=10,
                                   horizontal_flip=True,
                                   vertical_flip = True,
                                   fill_mode='nearest',
                                   data_format='channels_last'
                                   )
train_batches = train_datagen.flow_from_directory(DATASET_PATH + 'dogImages/train',
                                                  target_size=IMAGE_SIZE,
                                                  interpolation='bicubic',
                                                  class_mode='categorical',
                                                  shuffle=True,
                                                  batch_size=BATCH_SIZE)

valid_datagen = ImageDataGenerator()
valid_batches = valid_datagen.flow_from_directory(DATASET_PATH + 'dogImages/valid',
                                                  target_size=IMAGE_SIZE,
                                                  interpolation='bicubic',
                                                  class_mode='categorical',
                                                  shuffle=False,
                                                  batch_size=BATCH_SIZE)

# 输出各类别的索引值
for cls, idx in train_batches.class_indices.items():
    print('Class #{} = {}'.format(idx, cls))

# Create a basic model instance
net_final = create_model()

# 输出整个网路结构
print(net_final.summary())



if os.path.exists(DATASET_PATH):
    if os.path.exists(DATASET_PATH + WEIGHTS_FINAL):
        print(WEIGHTS_FINAL + "模型存在,将继续训练模型")
        # net_final.save(WEIGHTS_FINAL)
        new_net_final = load_model(WEIGHTS_FINAL)
        while True :
            new_net_final.fit(train_batches,
                              steps_per_epoch = train_batches.samples // BATCH_SIZE,
                              validation_data = valid_batches,
                              validation_steps = valid_batches.samples // BATCH_SIZE,
                              epochs = NUM_EPOCHS)#,
                              # callbacks = [LR_function])
            # 储存训练好的模型
            print("储存训练模型")
            new_net_final.save(WEIGHTS_FINAL) # .h5
            
    else:
        print(WEIGHTS_FINAL + '模型不存在,将新建训练模型')
        # 训练模型
        while True :
            net_final.fit(train_batches,
                          # validation_split = 0.2,
                          steps_per_epoch = train_batches.samples // BATCH_SIZE,
                          validation_data = valid_batches,
                          validation_steps = valid_batches.samples // BATCH_SIZE,
                          epochs = NUM_EPOCHS)#,
                          # callbacks = [LR_function])
            # 储存训练好的模型
            print("储存训练模型")
            net_final.save(WEIGHTS_FINAL) # .h5   
else:
    print(WEIGHTS_FINAL + '路径不存在,请确认路径')


<<:  Day-10 符号Symbol 跟字串String有什麽不同?

>>:  将自己的强项点好点满

Spring Framework X Kotlin Day 30 Review

GitHub Repo https://github.com/b2etw/Spring-Kotlin...

[Lesson5] BottomNavigationView

首先在res资料夹下,新增menu的资料夹 再来新增一个Menu resource file,取名为...

目录页 : 成为Canvas Ninja ~ 理解2D渲染的精髓

Day1 - 序言 - 成为Canvas Ninja ~ 理解2D渲染的精髓 基础篇 Day2 -...

Week37 -我当时害怕极了,原来Golang用指标是母汤的 [Server的终局之战系列]

大家好,在这个周末我参加了Golang-Conference-2020,每个议程养分都很高,而在最後...

学习javascript前...HTML3

学习html就是在学习如何使用标签,所以我现在要来了解各个标签的意思以及如何使用。 1.< t...