Showing
2 changed files
with
100 additions
and
0 deletions
ConvLSTM.ipynb
0 → 100644
1 | +{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"ConvLSTM.ipynb","provenance":[],"collapsed_sections":[],"authorship_tag":"ABX9TyMXox37jjcWOTMtoI++NxCW"},"kernelspec":{"display_name":"Python 3","name":"python3"},"language_info":{"name":"python"},"accelerator":"GPU"},"cells":[{"cell_type":"code","metadata":{"id":"KChrGBiCjFJF","executionInfo":{"status":"ok","timestamp":1623580255557,"user_tz":-540,"elapsed":2437,"user":{"displayName":"이혜리[학생](소프트웨어융합대학 컴퓨터공학과)","photoUrl":"","userId":"16579190241035414660"}}},"source":["import numpy as np\n","import cv2\n","import matplotlib.pyplot as plt\n","import os\n","import random\n","from math import ceil\n","from tqdm import tqdm_notebook\n","import tensorflow as tf\n","from keras.models import Sequential\n","from keras.layers import Dense\n","from keras.layers import Flatten\n","from keras.layers import Dropout\n","from keras.layers import LSTM\n","from keras.layers import TimeDistributed\n","from keras.layers import ConvLSTM2D\n","from keras.utils.np_utils import to_categorical\n","from keras.models import load_model"],"execution_count":3,"outputs":[]},{"cell_type":"code","metadata":{"id":"pRoXlH-ejqaV","executionInfo":{"status":"ok","timestamp":1623580304615,"user_tz":-540,"elapsed":361,"user":{"displayName":"이혜리[학생](소프트웨어융합대학 컴퓨터공학과)","photoUrl":"","userId":"16579190241035414660"}}},"source":["BASE_PATH = './Capston2'\n","DATASET_PATH = BASE_PATH+ '/data'"],"execution_count":6,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"MW6_oaLWFhXH"},"source":["### Image sequence를 numpy로 만들기"]},{"cell_type":"markdown","metadata":{"id":"27d5FeyVFyD4"},"source":["1. 각 image sequence의 data directory path 가져오고 train, test dataset 나누기"]},{"cell_type":"code","metadata":{"id":"WCxaqiIAkiJ8"},"source":["data_dir_path_list = []\n","for top, dir, f in os.walk(DATASET_PATH):\n"," if len(dir) == 0:\n"," data_dir_path_list.append(top)\n","\n","random.shuffle(data_dir_path_list)\n","\n","tr_ratio = 0.8\n","point = ceil(len(data_dir_path_list) * tr_ratio)\n","train_data_dir_path = data_dir_path_list[:point]\n","test_data_dir_path = data_dir_path_list[point:]\n","\n","# train_data_dir_path_numpy = np.array(train_data_dir_path)\n","# test_data_dir_path_numpy = np.array(test_data_dir_path)\n","# np.save(BASE_PATH + '/train_dir_path', train_data_dir_path_numpy)\n","# np.save(BASE_PATH + '/test_dir_path', test_data_dir_path_numpy)"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"Q7IcRpY-98LU"},"source":["2. image sequence를 5D tensor로 만들어서 저장"]},{"cell_type":"code","metadata":{"id":"3inQO4yQnOfp"},"source":["def get_image_sequence(data_dir_path_list, typ):\n"," idx = {'fight':0 , 'swoon':1, 'normal':2}\n"," dataset = []\n"," label = []\n"," for base_path in tqdm_notebook(data_dir_path_list):\n"," tmp = []\n"," im_list = sorted(os.listdir(base_path))\n"," for i in im_list:\n"," path = os.path.join(base_path,i)\n"," img = cv2.imread(path)\n"," img = cv2.resize(img, dsize=(0, 0), fx=0.045, fy=0.045, interpolation=cv2.INTER_AREA)\n"," tmp.append(np.array(img)) \n"," if len(tmp) == 20:\n"," dataset.append(np.array(tmp))\n"," label.append(idx[base_path.split('/')[-3]])\n","\n"," dataset = np.array(dataset)\n"," label = to_categorical(label)\n","\n"," np.save('./drive/MyDrive/Capston2/{}_img_seq'.format(typ), dataset)\n"," np.save('./drive/MyDrive/Capston2/{}_label'.format(typ), label)\n","\n","get_image_sequence(train_data_dir_path, 'train')\n","get_image_sequence(test_data_dir_path, 'test')"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"McUXDRXE-LHR"},"source":["### ConvLSTM model"]},{"cell_type":"code","metadata":{"id":"HWfLFz7g4cac"},"source":["# data load\n","train_img = np.load(BASE_PATH+'/train_img_seq.npy', allow_pickle = True)\n","train_label = np.load(BASE_PATH+'/train_label.npy', allow_pickle = True)\n","test_img = np.load(BASE_PATH+'/test_img_seq.npy', allow_pickle = True)\n","test_label = np.load(BASE_PATH+'/test_label.npy', allow_pickle = True)"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"YoOX36je18Bn","executionInfo":{"status":"ok","timestamp":1623509935114,"user_tz":-540,"elapsed":263,"user":{"displayName":"이혜리[학생](소프트웨어융합대학 컴퓨터공학과)","photoUrl":"","userId":"16579190241035414660"}},"outputId":"3e639a0d-dc89-43dd-bec6-eedf404c6f93"},"source":["print(train_img.shape)\n","print(train_label.shape)\n","print(test_img.shape)\n","print(test_label.shape)"],"execution_count":null,"outputs":[{"output_type":"stream","text":["(1994, 20, 97, 173, 3)\n","(1994, 3)\n","(401, 20, 97, 173, 3)\n","(401, 3)\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"xt6UbAh03aRj"},"source":["# fit and evaluate a model\n","def evaluate_model(trainX, trainy, testX, testy):\n"," epochs, batch_size = 10, 20\n"," n_steps = 20\n"," n_outputs = trainy.shape[1] # 3\n","\n"," # define model\n"," model = Sequential()\n"," model.add(ConvLSTM2D(filters=32, kernel_size=(3,3), activation='relu', data_format = \"channels_last\", input_shape=(n_steps, trainX.shape[2], trainX.shape[3], 3)))\n"," model.add(Dropout(0.5))\n"," model.add(Flatten())\n"," model.add(Dense(100, activation='relu'))\n"," model.add(Dense(n_outputs, activation='softmax'))\n","\n"," model.summary()\n","\n"," model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy', tf.keras.metrics.Recall(), tf.keras.metrics.Precision()])\n"," # fit network\n"," model.fit(trainX, trainy, epochs=epochs, batch_size=batch_size, verbose=1, validation_split=0.15, shuffle=True)\n"," # model save\n"," model.save(BASE_PATH+'/convLSTM.h5')\n","\t# evaluate model\n"," accuracy = model.evaluate(testX, testy, batch_size=batch_size, verbose=1)\n"," return accuracy\n","\n","acc = evaluate_model(train_img, train_label, test_img, test_label)\n","print('test accuracy: %.2f%%', % (acc[1]*100))"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"QCjP5lXs_Loe"},"source":["### 데이터 시험해보기"]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/","height":942,"output_embedded_package_id":"1IWQL7D1th7TXlyWnXlZ5a6wDWOo7I5Wr"},"id":"yGoPognn4hC3","executionInfo":{"status":"ok","timestamp":1623503736985,"user_tz":-540,"elapsed":15093,"user":{"displayName":"이혜리[학생](소프트웨어융합대학 컴퓨터공학과)","photoUrl":"","userId":"16579190241035414660"}},"outputId":"c4e5ee26-6e99-4ccb-9fb7-145b614d1ba5"},"source":["model = load_model(BASE_PATH+'/convLSTM.h5')\n","img_number = 400\n","sc = model.evaluate(test_img[img_number:img_number+1], test_label[img_number:img_number+1], verbose=0)\n","print(\"%s: %.2f%%\" % (model.metrics_names[1], sc[1]*100))\n","print(\"실제 label: \", test_label[img_number])\n","\n","fig=plt.figure(figsize=(27, 13))\n","rows = 4\n","cols = 5\n","for i in range(20):\n"," im = cv2.resize(test_img[img_number][i], dsize=(320, 180), interpolation=cv2.INTER_LINEAR)\n"," fig.add_subplot(rows, cols, i+1)\n"," plt.imshow(im)\n","\n","plt.tight_layout()\n","plt.show()"],"execution_count":null,"outputs":[{"output_type":"display_data","data":{"text/plain":"Output hidden; open in https://colab.research.google.com to view."},"metadata":{}}]}]} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
video_to_image_sequence.ipynb
0 → 100644
1 | +{ | ||
2 | + "cells": [ | ||
3 | + { | ||
4 | + "cell_type": "code", | ||
5 | + "execution_count": 1, | ||
6 | + "metadata": {}, | ||
7 | + "outputs": [], | ||
8 | + "source": [ | ||
9 | + "import os\n", | ||
10 | + "import random\n", | ||
11 | + "import cv2\n", | ||
12 | + "from PIL import Image" | ||
13 | + ] | ||
14 | + }, | ||
15 | + { | ||
16 | + "cell_type": "code", | ||
17 | + "execution_count": 2, | ||
18 | + "metadata": {}, | ||
19 | + "outputs": [], | ||
20 | + "source": [ | ||
21 | + "action = 'fight'\n", | ||
22 | + "base_dir = '/mnt/c/Users/hyerilee/Downloads/{}'.format(action)\n", | ||
23 | + "file_path = {}" | ||
24 | + ] | ||
25 | + }, | ||
26 | + { | ||
27 | + "cell_type": "code", | ||
28 | + "execution_count": 13, | ||
29 | + "metadata": {}, | ||
30 | + "outputs": [], | ||
31 | + "source": [ | ||
32 | + "for (root ,dirs, files) in os.walk(base_dir):\n", | ||
33 | + " if len(dirs) == 0 and len(files) != 0:\n", | ||
34 | + " if root not in file_path:\n", | ||
35 | + " file_path[root] = files" | ||
36 | + ] | ||
37 | + }, | ||
38 | + { | ||
39 | + "cell_type": "code", | ||
40 | + "execution_count": 75, | ||
41 | + "metadata": {}, | ||
42 | + "outputs": [], | ||
43 | + "source": [ | ||
44 | + "def getFrame(sec, saveFilePath, count):\n", | ||
45 | + " vidcap.set(cv2.CAP_PROP_POS_MSEC,sec*1000)\n", | ||
46 | + " hasFrames,img = vidcap.read()\n", | ||
47 | + " if hasFrames:\n", | ||
48 | + " img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n", | ||
49 | + " im_pil = Image.fromarray(img) # wsl에서 cv2.imwirte 안돼서 하는 작업. cv2 to pillow\n", | ||
50 | + " if os.path.exists(saveFilePath) and count==1: #동일한 시작지점을 또 capture하는 경우 skip\n", | ||
51 | + " return False\n", | ||
52 | + " if not os.path.exists(saveFilePath):\n", | ||
53 | + " os.makedirs(saveFilePath)\n", | ||
54 | + " im_pil.save(saveFilePath+'/'+'{:03d}'.format(count)+\".jpg\")\n", | ||
55 | + "# cv2.imwrite(saveFilePath+'{:03d}'.format(count)+\".jpg\", image) # save frame as JPG file\n", | ||
56 | + " return hasFrames\n", | ||
57 | + "\n", | ||
58 | + "starts = [i for i in range(180,291)] #가능시작지점\n", | ||
59 | + "for (file_dir, file_list) in file_path.items():\n", | ||
60 | + " for video_file in file_list:\n", | ||
61 | + " video_path = file_dir + '/' + video_file\n", | ||
62 | + " vidcap = cv2.VideoCapture(video_path)\n", | ||
63 | + " for i in range(0,10):\n", | ||
64 | + " sec = random.choice(starts)\n", | ||
65 | + " start_sec = sec\n", | ||
66 | + " frameRate = 0.5 # it will capture image in each 1 second\n", | ||
67 | + " cnt = 1\n", | ||
68 | + " success = getFrame(sec, video_path[:-4]+'_start'+str(start_sec)+'s', cnt)\n", | ||
69 | + " while success:\n", | ||
70 | + " cnt += 1\n", | ||
71 | + " sec += frameRate\n", | ||
72 | + " success = getFrame(sec, video_path[:-4]+'_start'+str(start_sec)+'s', cnt)\n", | ||
73 | + " if cnt >= 20:\n", | ||
74 | + " break" | ||
75 | + ] | ||
76 | + } | ||
77 | + ], | ||
78 | + "metadata": { | ||
79 | + "kernelspec": { | ||
80 | + "display_name": "Python 3", | ||
81 | + "language": "python", | ||
82 | + "name": "python3" | ||
83 | + }, | ||
84 | + "language_info": { | ||
85 | + "codemirror_mode": { | ||
86 | + "name": "ipython", | ||
87 | + "version": 3 | ||
88 | + }, | ||
89 | + "file_extension": ".py", | ||
90 | + "mimetype": "text/x-python", | ||
91 | + "name": "python", | ||
92 | + "nbconvert_exporter": "python", | ||
93 | + "pygments_lexer": "ipython3", | ||
94 | + "version": "3.6.13" | ||
95 | + } | ||
96 | + }, | ||
97 | + "nbformat": 4, | ||
98 | + "nbformat_minor": 4 | ||
99 | +} |
-
Please register or login to post a comment