下記のリンクのResnetを実装したのですが、モデルを確認しても層の数Resnet101やReset50など数字の部分の数え方が分かりません。
Link
様々なサイトを見たのですがいまいちどの部分を数えるのかが分かりませんでした。
どの部分を数えるのか、今回の場合にはResnetいくつになるのか教えてください。
# 必要なライブラリーのインストール import tensorflow as tf import tensorflow.keras as keras from tensorflow.keras.layers import Input from tensorflow.keras.layers import Activation from tensorflow.keras.layers import Concatenate from tensorflow.keras.models import Model from tensorflow.keras.layers import Conv2D from tensorflow.keras.layers import BatchNormalization from tensorflow.keras.layers import MaxPooling2D from tensorflow.keras.layers import Flatten from tensorflow.keras.layers import Dense from tensorflow.keras.layers import Dropout from tensorflow.keras.datasets import cifar10 import numpy as np from keras.callbacks import EarlyStopping, ModelCheckpoint print('tf :', tf.__version__) print('tf.keras :', tf.keras.__version__) print('keras :', keras.__version__) saveDir = "/home/script/saveResnet02/" # CIFAR10のデータを取得して、ベクトルに変換するクラス class CIFAR10Dataset(): def __init__(self): self.image_shape = (32, 32, 3) self.num_classes = 10 # 学習データとテストデータを取得する。 def get_batch(self): (x_train, y_train), (x_test, y_test) = cifar10.load_data() x_train, x_test = [self.change_vec(img_data) for img_data in [x_train, x_test]] y_train, y_test = [self.change_vec(img_data, label=True) for img_data in [y_train, y_test]] return x_train, y_train, x_test, y_test # 目的変数の場合は、クラスベクトルに変更する。説明変数は標準化する。 def change_vec(self, img_data, label=False): if label: data = keras.utils.to_categorical(img_data, self.num_classes) else: img_data = img_data.astype("float32") img_data /= 255 shape = (img_data.shape[0],) + self.image_shape img_data = img_data.reshape(shape) return img_data # ディープラーニングのモデルを設定して返す関数 def network(input_shape, num_classes, count): filter_count = 32 inputs = Input(shape=input_shape) x = Conv2D(32, kernel_size=3, padding="same", activation="relu")(inputs) x = BatchNormalization()(x) for i in range(count): shutcut = x #ショートカットコネクション用にモジュールの入力データを取得する。 x = Conv2D(filter_count, kernel_size=3, padding="same")(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = Dropout(rate=0.3)(x) x = Conv2D(filter_count, kernel_size=3, padding="same")(x) x = BatchNormalization()(x) x = Concatenate()([x, shutcut]) #ショートカットコネクション if i != count - 1: x = MaxPooling2D(pool_size=2)(x) filter_count = filter_count * 2 x = Flatten()(x) x = BatchNormalization()(x) x = Dense(1024, activation="relu")(x) x = Dropout(rate=0.3)(x) x = BatchNormalization()(x) x = Dense(1024, activation="relu")(x) x = Dropout(rate=0.3)(x) x = BatchNormalization()(x) x = Dense(num_classes, activation="softmax")(x) model = Model(inputs=inputs, outputs=x) print(model.summary()) return model # モデルを学習させるクラス class Trainer(): # モデルをコンパイルして、学習するための設定をプライベートプロパティに設定する。 def __init__(self, model, loss, optimizer): self._model = model self._model.compile( loss=loss, optimizer=optimizer, metrics=["accuracy"] ) self._verbose = 1 self._batch_size = 128 self._epochs = 100 # 実際の学習 def fit(self, x_train, y_train, x_test, y_test): chkpt = saveDir + 'Cifar10_Resnet_weights.{epoch:02d}-{loss:.2f}-{val_loss:.2f}.hdf5' cp_cb = ModelCheckpoint(filepath = chkpt, monitor='val_loss', verbose=1, save_best_only=True, mode='auto') self._model.fit( x_train, y_train, batch_size=self._batch_size, epochs=self._epochs, verbose=self._verbose, validation_data=(x_test, y_test), callbacks=[cp_cb] ) return self._model dataset = CIFAR10Dataset() # データを取得するためのCIFAR10Datasetのインスタンス化 model = network(dataset.image_shape, dataset.num_classes, 4) #モデルの取得 x_train, y_train, x_test, y_test = dataset.get_batch() # 学習データとテストデータの取得 trainer = Trainer(model, loss="sparse_categorical_crossentropy", optimizer="adam") # モデルとロス関数、最適化アルゴリズムを引数にして、Trainerのインスタンス化 model = trainer.fit(x_train, y_train, x_test, y_test) # モデルの学習 # モデルの評価 score = model.evaluate(x_test, y_test, verbose=0) print('Test loss: ', score[0]) print('Test accuracy: ', score[1])
https://www.bigdata-navi.com/aidrops/2611/
の「下図がモデルの概要図です」の34層の図で、長方形が34個あります
それと、その下の表を見比べたら、数え方が分かると思います
今回の場合は8層ということでお間違いないですか?
私が紹介したWebページの34層の図を見ると、長方形が塗られている色が何種類かありますよね
その図と、図の下の表の34層の部分を見比べると、図の長方形の色が、表の枠と対応していることが分かります
たとえば、図の一番上の長方形は同じ色の長方形が他にありませんが、それは表の「conv1」に対応してます
図のそのすぐ下に同じ色の長方形が6個あり、それは表の「conv2_x」です
以下、同様に続きます
図の一番下の長方形だけ分かりにくいのですが、表の「average ... fc, softmax」の「fc」です
図の34個の長方形が対応している表の要素の層数を書き出すと、下記の通りです
conv1: 1層
conv2_x: 2x3=6層
conv3_x: 2x4=8層
conv4_x: 2x6=12層
conv5_x: 2x3=6層
fc: 1層
以上を全部足すと、1+6+8+12+6+1=34層
上記の数え方を、質問者さんのコードに適用したら、
> 今回の場合は8層
ではないことが分かると思います
では何層かは、自分で数えてください
上記の数え方を適用したら、すぐに分かると思います
ただし、ResNet*は論文の表ではすべて最後の「fc」が1つのようですが、質問者さんのコードでは3つあります
その場合にそれを3層として数えていいのかは、私には分かりません
回答1件
あなたの回答
tips
プレビュー