본문 바로가기
Docker

도커로 파이썬 코드 실행하기, Run python code with docker

by 이든Eden 2021. 5. 30.

DVC라는 것과 관련해 프로젝트를 하나 진행 시작했는데, 딥러닝 모델들을 많이 만들어야 한다. 그래서 케라스 예제의 모델들을 가져와서 테스트 해보고 코드를 정리해서 올리기로 했다.

그래서 코드를 tf.keras로 구현 & 클래스로 정리 &  잘 돌아가는지 확인, 이 세가지를 하기 위해 도커로 코드를 실행하려고 계획했다. 그 과정을 아주 간단하게 정리하려고 한다.

 

순서는 아래와 같이 정리된다.

1. Dockerfile을 만든다
2. model.py를 만든다
3. Dockerfile을 빌드해서 이미지를 만든다
4. 이미지를 run하여 model.py를 실행한다

 

 

1. Dockerfile을 만든다

나는 tensorflow:2.5.0 베이스로 Dockerfile을 만들었고, requirements.txt에 opencv-python 등 간단한 것을 적어두었다. 

model.py라는 파일을 나중에 테스트 해볼 예정이기 때문에 CMD, ENTRYPOINT를 설정해둔다.

FROM tensorflow/tensorflow:2.5.0

WORKDIR /usr/src

COPY ./requirements.txt .

RUN pip install --no-cache-dir --upgrade pip && \
    pip install -r requirements.txt

COPY . .

CMD ["model.py"]

ENTRYPOINT ["python3"]

 

 

2. model.py를 만든다

특별한 것 없이 그냥 딥러닝 모델을 summary로 확인해보는 코드

import tensorflow as tf
import tensorflow.keras.layers as layers

class ClassificationModel:
	def __init__(self, image_size, num_classes):
		self.input_shape = image_size + (3,)
		self.num_classes = num_classes

	def build_model(self):
		def data_augmentation(inputs):
			data_augmentation = tf.keras.Sequential(
				[
					layers.experimental.preprocessing.RandomFlip("horizontal"),
					layers.experimental.preprocessing.RandomRotation(0.1),
				]
			)
			return data_augmentation(inputs)

		inputs = tf.keras.Input(self.input_shape)

		x = data_augmentation(inputs)
		x = layers.experimental.preprocessing.Rescaling(1.0 / 255)(x)
		x = layers.Conv2D(32, 3, strides=2, padding="same")(x)
		x = layers.BatchNormalization()(x)
		x = layers.Activation("relu")(x)

		x = layers.Conv2D(64, 3, padding="same")(x)
		x = layers.BatchNormalization()(x)
		x = layers.Activation("relu")(x)

		previous_block_activation = x  # Set aside residual

		for size in [128, 256, 512, 728]:
			x = layers.Activation("relu")(x)
			x = layers.SeparableConv2D(size, 3, padding="same")(x)
			x = layers.BatchNormalization()(x)

			x = layers.Activation("relu")(x)
			x = layers.SeparableConv2D(size, 3, padding="same")(x)
			x = layers.BatchNormalization()(x)

			x = layers.MaxPooling2D(3, strides=2, padding="same")(x)

			# Project residual
			residual = layers.Conv2D(size, 1, strides=2, padding="same")(
				previous_block_activation
			)
			x = layers.add([x, residual])  # Add back residual
			previous_block_activation = x  # Set aside next residual

		x = layers.SeparableConv2D(1024, 3, padding="same")(x)
		x = layers.BatchNormalization()(x)
		x = layers.Activation("relu")(x)

		x = layers.GlobalAveragePooling2D()(x)
		if self.num_classes == 2:
			activation = "sigmoid"
			units = 1
		else:
			activation = "softmax"
			units = self.num_classes

		x = layers.Dropout(0.5)(x)
		outputs = layers.Dense(units, activation=activation)(x)
		return tf.keras.Model(inputs, outputs)


if __name__ == "__main__":
	image_size = (180, 180)
	num_classes = 2

	model = ClassificationModel(image_size=image_size, num_classes=num_classes)
	model = model.build_model()
	print(model.summary())

 

 

3. Dockerfile을 빌드해서 이미지를 만든다

image_name:tag와 함께 Dockerfile을 빌드한다.

$ docker build -t keras-model:0.1 .

 

 

4. 이미지를 run하여 model.py를 실행한다

도커 이미지로 model.py를 실행해본다. 실행 결과까지 아래에서 보여준다.

$ docker run -it keras-model:0.1 model.py
2021-05-30 12:49:22.007718: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
Model: "model"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            [(None, 180, 180, 3) 0                                            
__________________________________________________________________________________________________
sequential (Sequential)         (None, 180, 180, 3)  0           input_1[0][0]                    
__________________________________________________________________________________________________
rescaling (Rescaling)           (None, 180, 180, 3)  0           sequential[0][0]                 
__________________________________________________________________________________________________
conv2d (Conv2D)                 (None, 90, 90, 32)   896         rescaling[0][0]                  
__________________________________________________________________________________________________
batch_normalization (BatchNorma (None, 90, 90, 32)   128         conv2d[0][0]                     
__________________________________________________________________________________________________
activation (Activation)         (None, 90, 90, 32)   0           batch_normalization[0][0]        
__________________________________________________________________________________________________
conv2d_1 (Conv2D)               (None, 90, 90, 64)   18496       activation[0][0]                 
__________________________________________________________________________________________________
batch_normalization_1 (BatchNor (None, 90, 90, 64)   256         conv2d_1[0][0]                   
__________________________________________________________________________________________________
activation_1 (Activation)       (None, 90, 90, 64)   0           batch_normalization_1[0][0]      
__________________________________________________________________________________________________
activation_2 (Activation)       (None, 90, 90, 64)   0           activation_1[0][0]               
__________________________________________________________________________________________________
separable_conv2d (SeparableConv (None, 90, 90, 128)  8896        activation_2[0][0]               
__________________________________________________________________________________________________
batch_normalization_2 (BatchNor (None, 90, 90, 128)  512         separable_conv2d[0][0]           
__________________________________________________________________________________________________
activation_3 (Activation)       (None, 90, 90, 128)  0           batch_normalization_2[0][0]      
__________________________________________________________________________________________________
separable_conv2d_1 (SeparableCo (None, 90, 90, 128)  17664       activation_3[0][0]               
__________________________________________________________________________________________________
batch_normalization_3 (BatchNor (None, 90, 90, 128)  512         separable_conv2d_1[0][0]         
__________________________________________________________________________________________________
max_pooling2d (MaxPooling2D)    (None, 45, 45, 128)  0           batch_normalization_3[0][0]      
__________________________________________________________________________________________________
conv2d_2 (Conv2D)               (None, 45, 45, 128)  8320        activation_1[0][0]               
__________________________________________________________________________________________________
add (Add)                       (None, 45, 45, 128)  0           max_pooling2d[0][0]              
                                                                 conv2d_2[0][0]                   
__________________________________________________________________________________________________
activation_4 (Activation)       (None, 45, 45, 128)  0           add[0][0]                        
__________________________________________________________________________________________________
separable_conv2d_2 (SeparableCo (None, 45, 45, 256)  34176       activation_4[0][0]               
__________________________________________________________________________________________________
batch_normalization_4 (BatchNor (None, 45, 45, 256)  1024        separable_conv2d_2[0][0]         
__________________________________________________________________________________________________
activation_5 (Activation)       (None, 45, 45, 256)  0           batch_normalization_4[0][0]      
__________________________________________________________________________________________________
separable_conv2d_3 (SeparableCo (None, 45, 45, 256)  68096       activation_5[0][0]               
__________________________________________________________________________________________________
batch_normalization_5 (BatchNor (None, 45, 45, 256)  1024        separable_conv2d_3[0][0]         
__________________________________________________________________________________________________
max_pooling2d_1 (MaxPooling2D)  (None, 23, 23, 256)  0           batch_normalization_5[0][0]      
__________________________________________________________________________________________________
conv2d_3 (Conv2D)               (None, 23, 23, 256)  33024       add[0][0]                        
__________________________________________________________________________________________________
add_1 (Add)                     (None, 23, 23, 256)  0           max_pooling2d_1[0][0]            
                                                                 conv2d_3[0][0]                   
__________________________________________________________________________________________________
activation_6 (Activation)       (None, 23, 23, 256)  0           add_1[0][0]                      
__________________________________________________________________________________________________
separable_conv2d_4 (SeparableCo (None, 23, 23, 512)  133888      activation_6[0][0]               
__________________________________________________________________________________________________
batch_normalization_6 (BatchNor (None, 23, 23, 512)  2048        separable_conv2d_4[0][0]         
__________________________________________________________________________________________________
activation_7 (Activation)       (None, 23, 23, 512)  0           batch_normalization_6[0][0]      
__________________________________________________________________________________________________
separable_conv2d_5 (SeparableCo (None, 23, 23, 512)  267264      activation_7[0][0]               
__________________________________________________________________________________________________
batch_normalization_7 (BatchNor (None, 23, 23, 512)  2048        separable_conv2d_5[0][0]         
__________________________________________________________________________________________________
max_pooling2d_2 (MaxPooling2D)  (None, 12, 12, 512)  0           batch_normalization_7[0][0]      
__________________________________________________________________________________________________
conv2d_4 (Conv2D)               (None, 12, 12, 512)  131584      add_1[0][0]                      
__________________________________________________________________________________________________
add_2 (Add)                     (None, 12, 12, 512)  0           max_pooling2d_2[0][0]            
                                                                 conv2d_4[0][0]                   
__________________________________________________________________________________________________
activation_8 (Activation)       (None, 12, 12, 512)  0           add_2[0][0]                      
__________________________________________________________________________________________________
separable_conv2d_6 (SeparableCo (None, 12, 12, 728)  378072      activation_8[0][0]               
__________________________________________________________________________________________________
batch_normalization_8 (BatchNor (None, 12, 12, 728)  2912        separable_conv2d_6[0][0]         
__________________________________________________________________________________________________
activation_9 (Activation)       (None, 12, 12, 728)  0           batch_normalization_8[0][0]      
__________________________________________________________________________________________________
separable_conv2d_7 (SeparableCo (None, 12, 12, 728)  537264      activation_9[0][0]               
__________________________________________________________________________________________________
batch_normalization_9 (BatchNor (None, 12, 12, 728)  2912        separable_conv2d_7[0][0]         
__________________________________________________________________________________________________
max_pooling2d_3 (MaxPooling2D)  (None, 6, 6, 728)    0           batch_normalization_9[0][0]      
__________________________________________________________________________________________________
conv2d_5 (Conv2D)               (None, 6, 6, 728)    373464      add_2[0][0]                      
__________________________________________________________________________________________________
add_3 (Add)                     (None, 6, 6, 728)    0           max_pooling2d_3[0][0]            
                                                                 conv2d_5[0][0]                   
__________________________________________________________________________________________________
separable_conv2d_8 (SeparableCo (None, 6, 6, 1024)   753048      add_3[0][0]                      
__________________________________________________________________________________________________
batch_normalization_10 (BatchNo (None, 6, 6, 1024)   4096        separable_conv2d_8[0][0]         
__________________________________________________________________________________________________
activation_10 (Activation)      (None, 6, 6, 1024)   0           batch_normalization_10[0][0]     
__________________________________________________________________________________________________
global_average_pooling2d (Globa (None, 1024)         0           activation_10[0][0]              
__________________________________________________________________________________________________
dropout (Dropout)               (None, 1024)         0           global_average_pooling2d[0][0]   
__________________________________________________________________________________________________
dense (Dense)                   (None, 1)            1025        dropout[0][0]                    
==================================================================================================
Total params: 2,782,649
Trainable params: 2,773,913
Non-trainable params: 8,736
__________________________________________________________________________________________________
None

 

 

짜잔! 이런 방법으로 도커환경을 이용하여 파이썬 파일을 실행해볼 수 있다.

 

'Docker' 카테고리의 다른 글

Pycharm과 Docker 연동  (2) 2020.05.03