# パッケージのインポート from tensorflow.keras.datasets import cifar10 from tensorflow.keras.callbacks import LearningRateScheduler from tensorflow.keras.layers import Activation, Add, BatchNormalization, Conv2D, Dense, GlobalAveragePooling2D, Input from tensorflow.keras.models import Model from tensorflow.keras.optimizers import SGD from tensorflow.keras.preprocessing.image import ImageDataGenerator from tensorflow.keras.regularizers import l2 from tensorflow.keras.utils import to_categorical import numpy as np import matplotlib.pyplot as plt %matplotlib inline
# 残差ブロックAの生成 deffirst_residual_unit(filters, strides): deff(x): # →BN→ReLU x = BatchNormalization()(x) b = Activation('relu')(x)
# 畳み込み層→BN→ReLU x = conv(filters // 4, 1, strides)(b) x = BatchNormalization()(x) x = Activation('relu')(x) # 畳み込み層→BN→ReLU x = conv(filters // 4, 3)(x) x = BatchNormalization()(x) x = Activation('relu')(x)
# 残差ブロックBの生成 defresidual_unit(filters): deff(x): sc = x # →BN→ReLU x = BatchNormalization()(x) x = Activation('relu')(x) # 畳み込み層→BN→ReLU x = conv(filters // 4, 1)(x) x = BatchNormalization()(x) x = Activation('relu')(x) # 畳み込み層→BN→ReLU x = conv(filters // 4, 3)(x) x = BatchNormalization()(x) x = Activation('relu')(x) # 畳み込み層→ x = conv(filters, 1)(x)
# Add return Add()([x, sc]) return f
残差ブロックAと残差ブロックB x 17を生成します。
1 2 3 4 5 6 7 8
# 残差ブロックAと残差ブロックB x 17の生成 defresidual_block(filters, strides, unit_size): deff(x): x = first_residual_unit(filters, strides)(x) for i inrange(unit_size-1): x = residual_unit(filters)(x) return x return f
# LearningRateSchedulerの準備 defstep_decay(epoch): x = 0.1 if epoch >= 80: x = 0.01 if epoch >= 120: x = 0.001 return x lr_decay = LearningRateScheduler(step_decay)