実現したいこと
https://qiita.com/notfolder/items/31bb8be368239914a622
こちらの記事を参考に自分の学習の結果を表示させたいと思い、見よう見まねでやってみたもののメモリが足りなくなり動きません
自分のコードで動かせるようにはどうすればいいか知りたいです
該当のソースコード
Projector用のコードは記述してないものを記述します
python
1import tensorflow as tf 2from tensorflow.keras import backend, models, layers, regularizers , optimizers 3from tensorflow.keras.models import Model, Sequential 4from tensorflow.keras.layers import BatchNormalization , Concatenate 5from keras.layers import Activation, Flatten, Dense, Dropout ,Input 6from tensorflow.keras.layers import Conv2D, MaxPooling2D, AveragePooling2D 7from tensorflow.keras.utils import to_categorical 8from tensorflow.keras.metrics import categorical_crossentropy 9from tensorflow.keras.callbacks import EarlyStopping 10from sklearn.model_selection import train_test_split 11from tensorflow.keras.preprocessing import image 12from tensorflow.keras.preprocessing.image import ImageDataGenerator 13from tensorflow.keras.applications import InceptionV3 14from tensorflow.keras.utils import plot_model 15from tensorflow.keras import backend 16import matplotlib.image as mpimg 17import matplotlib.pyplot as plt 18%matplotlib inline 19import random 20import os,datetime 21import numpy as np 22import pandas as pd 23 24train_path = "../train" 25valid_path = "../valid" 26test_path = "../test" 27 28train_datagen_augmented = ImageDataGenerator( 29 rescale=1/255, 30 horizontal_flip=True, # 水平反転 31 rotation_range=60, # 回転 32 zoom_range=0.1, # 拡大 33 width_shift_range =0.5, #左右水平移動 34 height_shift_range =0.5, #上下水平移動 35 shear_range = 30, #せん断 36 fill_mode = "nearest" , #補完 37) 38valid_datagen = ImageDataGenerator( 39 rescale=1/255 40 ) 41test_datagen = ImageDataGenerator( 42 rescale=1/255 43 ) 44 45train_generator_augmented = train_datagen_augmented.flow_from_directory( 46 train_path, 47 target_size=(224, 224), 48 batch_size=32, 49 color_mode="rgb", 50 class_mode="sparse", 51 shuffle=True, 52) 53validation_generator = valid_datagen.flow_from_directory( 54 valid_path, 55 target_size=(224, 224), 56 batch_size=32, 57 color_mode="rgb", 58 class_mode="sparse") 59 60test_generator = test_datagen.flow_from_directory( 61 test_path, 62 target_size=(224, 224), 63 batch_size=32, 64 color_mode="rgb", 65 class_mode="sparse") 66 67# ここからモデル定義とか 68backend.clear_session() 69 70# pre-trainモデルのload 71incbasemodel4 = InceptionV3(weights="imagenet", include_top=False, input_shape= (224, 224, 3)) 72finetune_at = 20 73incbasemodel4.trainable = True 74 75# 最上層あたり以外をフリーズ 76for layer in incbasemodel4.layers[:finetune_at - 1]: 77 layer.trainable = False 78 # Batch Normalization の freeze解除 79 if layer.name.startswith('batch_normalization'): 80 layer.trainable = True 81 82# Sequential に追加していく 83model_inceptionv3_finetune = models.Sequential() 84model_inceptionv3_finetune.add(incbasemodel4) 85model_inceptionv3_finetune.add(layers.GlobalAveragePooling2D()) 86model_inceptionv3_finetune.add(layers.Dense(1024, activation="relu")) 87model_inceptionv3_finetune.add(layers.Dense(20, activation="sigmoid")) 88 89model_inceptionv3_finetune.compile(optimizer=optimizers.Adam(), 90 loss="sparse_categorical_crossentropy", 91 metrics=["accuracy"]) 92 93checkpoint_path = "../checkpoints/cp-{epoch:04d}.ckpt" 94checkpoint_dir = os.path.dirname(checkpoint_path) 95 96# checkpoint保存用コールバック 97cp_callback =tf.keras.callbacks.ModelCheckpoint( 98 filepath=checkpoint_path, 99 verbose=1, 100 save_weights_only=True, 101 period=1 102) 103 104log_dir = "logs/fit/"+ datetime.datetime.now().strftime("%Y%m%d-%H%M%S") 105tb_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1, write_graph=True) 106 107history = model_inceptionv3_finetune.fit_generator( 108 train_generator_augmented, 109 epochs=100, 110 validation_data=validation_generator, 111 verbose=1, 112 shuffle=True, 113 callbacks=[ 114 EarlyStopping(monitor="val_accuracy", patience=10, restore_best_weights=True), 115 cp_callback, 116 tb_callback, 117 ] 118) 119
試したこと
colab上で動かしており、メモリが足りないといわれたため課金したのですがなお足りませんでした
25GBあるようです
あなたの回答
tips
プレビュー