Slide 1

Slide 1 text

AI   #1 (classification)

Slide 2

Slide 2 text

Keras  

Slide 3

Slide 3 text

 $# Google%   Jupyter notebook$# Google Colaboratory (https://colab.research.google.com/) 12! " GPU, TPU! Google Drive  &! … etc

Slide 4

Slide 4 text

Google Colab   PYTHON    Notebook  Colab!

Slide 5

Slide 5 text

(%"# )  :    !  : $ !   Notebook 

Slide 6

Slide 6 text

    $"  !%  GPU% #     GPU 

Slide 7

Slide 7 text

%# & ’shift + enter’    "$ !     

Slide 8

Slide 8 text

 $’!’ "  Linux   !# Linux 

Slide 9

Slide 9 text

ColabDrive&+ Google Drive!   drive" Colb)' drive"%*  from google.colab import drive drive.mount('/content/drive') "( google.colabdrive !  Drive.mount$#  Google Drive! 

Slide 10

Slide 10 text

   ' !# URL (&  ) Google Drive% "$ (1)

Slide 11

Slide 11 text

$ # %! & ' " (’enter’   Google Drive  (2)

Slide 12

Slide 12 text

E@$#%"! <; *(7- ?.=384 :E@ 96  <;7  <;&'!Slack> 1,  PLAYGROUND /  0C   D5)+A - $# -2B  

Slide 13

Slide 13 text

,89 Team Drives*(@2)/$#!7 "'   10Team Drives$#!5< (3:-  …) # [001] Install use items &% !.+  Team Drives $#!"' .+46 =;  >? # [002] Mount GoogleDrive # [003] Copy data from google drive # [004] Check directory structure

Slide 14

Slide 14 text

  Keras")* $&!    '&   % $  )*#(   )* $ 

Slide 15

Slide 15 text

:'<$  /1 !%   '<9 '<7 *)0   (C F ) '<!%  '<93"      !5 '<9> (,?) '<  8) CF'<9A'<   D+('24)(6.  8) FBC;&C'<-= (@+ #E)

Slide 16

Slide 16 text

Keras PythonA 8A =?7#&&/ (=?7   ) 59 (JH)  E : =?7 GCF

Slide 17

Slide 17 text

#$ # [005] Define constant value # [006] Make data file list # [007] Check dog data # [008] Check cat data    "   ! " ‘_t_XXX.png’ &(Train) ‘_v_XXX.png’  (Validation) ‘_e_XXX.png’ %(Evaluate)

Slide 18

Slide 18 text

+.0  )C57 ,? %98 /- %98%:=>E04 @) H*(:[1, 0], G*(:[0, 1] ,? ;F %98G or H OneHotVector )CA("$%&#!),? 6 2 %98'.(B1D ) numpy>E04<3 

Slide 19

Slide 19 text

!#'% KerasOneHotVactor -*' &#$  import keras.utils as ku class_ids = np.array([1, 0, 1, 1, 2, 0]) one_hots = ku.to_categorical(class_ids) print(one_hots) > [[0, 1, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 0, 0]] # [009] Make input and teacher data   "  (,cv2imread  Numpy)+ OK

Slide 20

Slide 20 text

!$ Keras!$ 1) Sequential " 2) functional API# "    Keras!$ "  2) &(  " 2)functional API# "'%  

Slide 21

Slide 21 text

#&) %   functional API$ % input_layer = Input((64, 64, 1)) cv_1_layer = Conv2D(32, 3)(input_layer) cv_2_layer = Conv2D(64, 3)(cv_1_layer) max_layer = MaxPooling2D()(cv_2_layer) flat_layer = Flatten()(max_layer) output_layer = Dense(2)(flat_layer) *'(" Keras !  

Slide 22

Slide 22 text

 Input&!    input_layer = Input((64, 64, 1)) ( Input(input_shape) #% input_shape '    $ "

Slide 23

Slide 23 text

Convolution!Conv2D1)  Convolution! cv_1_layer = Conv2D(32, 3)(input_layer) 4 Conv2D(filters, kernel_size, strides=(1, 1), padding=‘valid’, activation=None) *,- filters ( ) (30/%.-) kernel_size  - 5 #"+% strides  - 5 #"+% padding’valid’padding3 ’same’ 2('&$  activation

Slide 24

Slide 24 text

MaxPoolingMaxPooling2D.&  MaxPooling max_layer = MaxPooling2D()(cv_layer) 3 MaxPooling2D(pool_size=(2, 2), strides=None) ')* pool_size  *4 " (# strides  *4 " (# ,%2+$ . /5610-! 

Slide 25

Slide 25 text

 Flatten#   flat_layer = Flatten()(max_layer) % Flatten()      "! $& 

Slide 26

Slide 26 text

  Dense     output_layer = Dense(2)(flat_layer)  Dense(units, activation=None)   units  activation

Slide 27

Slide 27 text

  !  Activate   act_layer = Activate(activation='relu’)(cv_layer) " Activate(activation=None)   activation

Slide 28

Slide 28 text

;5F(%'- #! (%'- #!3D CB 14,"* (Input)!G.E7<,"*  9:input_shape==3A8 CB @?& $!6>/ 0/2 & $!)+%'( @?)A8  3D # [010] Define Network

Slide 29

Slide 29 text

 input_layer = Input(IMAGE_SHAPE) cv1_1 = Conv2D(32, 3, padding='same', input_shape=IMAGE_SHAPE)(input_layer) cv1_1 = Activation(activation='relu')(cv1_1) cv1_2 = Conv2D(32, 3, padding='same')(cv1_1) cv1_2 = Activation(activation='relu')(cv1_2) cv1_max = MaxPooling2D()(cv1_2) cv2_1 = Conv2D(64, 3, padding='same')(cv1_max) cv2_1 = Activation(activation='relu')(cv2_1) cv2_2 = Conv2D(64, 3, padding='same')(cv2_1) cv2_2 = Activation(activation='relu')(cv2_2) cv2_max = MaxPooling2D()(cv2_2) flat_layer = Flatten()(cv2_max) fc = Dense(2)(flat_layer) output_layer = Activation(activation='softmax')(fc)

Slide 30

Slide 30 text

 "  Model#  Keras  model = Model(inputs=[input_layer], outputs=[output_layer]) Model  !  inputs (  ) outputs   ( )(  )   % $  

Slide 31

Slide 31 text

()'#  ! & '# model.summary()  +*$ from keras.utils import plot_model plot_model(model, to_file=MODEL_PNG_NAME) to_file "  dot ()%

Slide 32

Slide 32 text

!-  !-      model.compile(optimizer=Adam(), loss='categorical_crossentropy', metrics=['accuracy']) " %&  optimizer   loss metrics " [’accuracy’]( $ ,#' +*& crossentropy) 

Slide 33

Slide 33 text

  fit   history = model.fit(train_inputs, train_teachers, batch_size=20, epochs=100 , validation_data=(valid_inputs, valid_teachers) , shuffle=True, verbose=1, callbacks=callbacks)   train_inputs$*#! train_teachers$*#') batch_size  epochs  % validation_data&(# (!, ')) shuffle!   verbose$*!" (1 ) callbacks %()

Slide 34

Slide 34 text

+ "&  + model.save_weights(save_file_name) $+ &  ' )*%#(!  model.save(save_file_name) )* $+ &  , & 

Slide 35

Slide 35 text

 +  # [012] Train     !*Adam )'$categorical_crossentropy & % (# +",

Slide 36

Slide 36 text

+./, # [013] Check loss and acc  '  &#(3loss41acc41$  )% loss((3loss) %   (3 )% val loss(*-loss) 5%  "! 0(32

Slide 37

Slide 37 text

0, 0,&"#$ +(  "#$ from keras.models import load_model model = load_model(model_file) !%-. '/  $    model = Model(inputs=[input_layer], outputs=[output_layer]) model.load_weights(model_file) )'/  $  "#-.* $ 

Slide 38

Slide 38 text

 predict   model.predict(predict_inputs)   predict_inputs  (  )       # [014] Load trained model    # [015] Predict (Train data)  # [016] Predict (Valid data) # [017] Predict (Evaluate data)

Slide 39

Slide 39 text

'43 51$ (-".  0  '43  , #!///+ )*//&2 1% //&2

Slide 40

Slide 40 text

,+#%3417  #%3417 0.68 !'" 5/ 2 3+-4+-Convolution&$* Dropout0.55/ .6  ,+0.6 ()    

Slide 41

Slide 41 text

  Dropout      drop_layer = Dropout()(cv_layer)  Dropout(rate)  rate

Slide 42

Slide 42 text

;JIL0'$)8H   "&*?@3 

Slide 43

Slide 43 text

# "  ModelCheckpoint " import keras.callbacks as kc kc.ModelCheckpoint( filepath=MODEL_FILE_NAME, save_weights_only=True , save_best_only=True, period=1)    filepath (  ) save_weights_only   save_best_only  period  ! val_loss!   

Slide 44

Slide 44 text

       AutoEncoder  etc …

Slide 45

Slide 45 text

     Keras