第5回は画像処理に有効な深層学習モデルを扱う。
from tensorflow import keras from tensorflow.keras.datasets import fashion_mnist (tr_img,tr_lbl),(tt_img,tt_lbl)=fashion_mnist.load_data()
import matplotlib.pyplot as plt
plt.imshow(tr_img[0],cmap='gray')
plt.show()
print("lbl:",tr_lbl[0])from tensorflow.keras import layers inputs=keras.Input(shape=(28,28,1)) x=layers.Conv2D(32,3,activation='relu')(inputs) x=layers.Conv2D(64,3,activation='relu')(x) x=layers.MaxPooling2D(2)(x) x=layers.Conv2D(64,3,activation='relu')(x) x=layers.Conv2D(128,3,activation='relu')(x) x=layers.MaxPooling2D(2)(x) x=layers.Conv2D(128,3,activation='relu')(x) x=layers.Flatten()(x) outputs=layers.Dense(10,activation='softmax')(x) model=keras.Model(inputs,outputs)
tr_img=tr_img.reshape((60000,28,28,1))
tr_img=tr_img.astype("float32")/255
tt_img=tt_img.reshape((10000,28,28,1))
tt_img=tt_img.astype("float32")/255model.compile(optimizer="rmsprop",
loss="sparse_categorical_crossentropy",
metrics=['accuracy'])
hist=model.fit(tr_img,tr_lbl,epochs=5,batch_size=64,
validation_data=(tt_img,tt_lbl),)import matplotlib.pyplot as plt
acc=hist.history["accuracy"]
val_acc=hist.history["val_accuracy"]
loss=hist.history["loss"]
val_loss=hist.history["val_loss"]
epochs=range(1,len(acc)+1)
plt.plot(epochs,acc,"bo",label="Tr-acc")
plt.plot(epochs,val_acc,"b",label="Val-acc")
plt.title("Training and Validation Acc")
plt.legend()
plt.figure()
plt.plot(epochs,loss,"bo",label="Tr-loss")
plt.plot(epochs,val_loss,"b",label="Val-loss")
plt.title("Training and Validation Loss")
plt.legend()
plt.show()!mkdir /root/.kaggle/ !cp kaggle.json /root/.kaggle/ !chmod 600 /root/.kaggle/kaggle.json
!kaggle competitions download -c dogs-vs-cats
DIR="/content/tmp" !mkdir $DIR %cd $DIR !git clone https://github.com/ultralytics/yolov5 %cd yolov5 !pip install -qr requirements.txt !wget https://github.com/ultralytics/yolov5/releases/download/v1.0/coco128.zip !unzip coco128.zip !python train.py --img 640 --batch 16 --epochs 300 --data coco128.yaml --weights yolov5x.pt
%load_ext tensorboard %tensorboard --logdir runs
!python detect.py --source data/images --weights yolov5x.pt --conf 0.50
!python train.py --img 640 --batch 16 --epochs 30 --data coco128.yaml --weights yolov5s.pt