text: # Oxford Pet Dataset 불러오기
  import numpy as np
  import tensorflow as tf
  import tensorflow_datasets as tfds
  
  dataset, info = tfds.load('oxford_iiit_pet:3.2.0', with_info=True)
  print(info)
  
  
  # train, test 데이터 수 저장
  train_data_len = info.splits['train'].num_examples
  test_data_len = info.splits['test'].num_examples
  
  
  # 이미지 로드 함수 정의
  def load_image(datapoint):
      img = tf.image.resize(datapoint['image'], (128, 128))
      mask = tf.image.resize(datapoint['segmentation_mask'], (128, 128))
      img = tf.cast(img, tf.float32)
      img = img/255.0
      # 원래 데이터는 1,2,3값으로 But, 우리는 0,1,2로 해야하기 때문데
      mask -= 1
  
      return img, mask
  
  # train, test Dataset 정의
  train_dataset = dataset['train'].map(load_image)
  train_dataset = train_dataset.repeat()
  train_dataset = train_dataset.batch(16)
  
  test_dataset = dataset['test'].map(load_image)
  test_dataset = test_dataset.repeat()
  test_dataset = test_dataset.batch(1)
  
  # 이미지, 마스크 확인
  import matplotlib.pyplot as plt
  for img, mask in train_dataset.take(1):
      plt.figure(figsize=(10, 5))
  
      plt.subplot(1, 2, 1)
      plt.imshow(img[2])
  
      plt.subplot(1, 2, 2)
      plt.imshow(np.squeeze(mask[2], axis=2))
      plt.colorbar()
  plt.show()
  
  
  # Segmentation을 위한 REDNet 네트워크 정의
  def REDNet_segmentation(num_layers):
      conv_layers = []
      deconv_layers = []
      residual_layers = []
  
      inputs = tf.keras.layers.Input(shape=(None, None, 3))
      conv_layers.append(tf.keras.layers.Conv2D(3, kernel_size=3, padding='same', activation='relu'))
      for i in range(num_layers - 1):
          conv_layers.append(tf.keras.layers.Conv2D(64, kernel_size=3, padding='same', activation='relu'))
          deconv_layers.append(tf.keras.layers.Conv2DTranspose(64, kernel_size=3, padding='same', activation='relu'))
      deconv_layers.append(tf.keras.layers.Conv2DTranspose(3, kernel_size=3, padding='same', activation='softmax'))
      x = conv_layers[0](inputs)
      for i in range(num_layers - 1):
          x = conv_layers[i + 1](x)
          if i % 2 == 0:
              residual_layers.append(x)
      for i in range(num_layers - 1):
          if i % 2 == 1:
              x = tf.keras.layers.Add()([x, residual_layers.pop()])
              x = tf.keras.layers.Activation('relu')(x)
          x = deconv_layers[i](x)
      x = deconv_layers[-1](x)
      model = tf.keras.Model(inputs=inputs, outputs=x)
      return model
  
  
  # Segmentation을 위한 REDNet 네트워크 초기화 및 컴파일
  model = REDNet_segmentation(15)
  model.compile(optimizer=tf.optimizers.Adam(0.0001),
                loss='sparse_categorical_crossentropy',
                metrics=['accuracy'])
  
  model.summary()
  
  # Segmentation을 위한 REDNet 네트워크 학습
  history = model.fit(train_dataset,
                      epochs=1,
                      steps_per_epoch=train_data_len//16,
                      validation_data=test_dataset,
                      validation_steps=test_data_len)
  
  # 테스트 이미지 분할 확인
  plt.figure(figsize=(12, 12))
  for idx, (img, mask) in enumerate(test_dataset.take(3)):
      plt.subplot(3, 3, idx*3+1)
      plt.imshow(img[0])
  
      plt.subplot(3, 3, idx*3+2)
      plt.imshow(np.squeeze(mask[0], axis=2))
  
      predict = tf.argmax(model.predict(img), axis=-1)
      plt.subplot(3, 3, idx*3+3)
      plt.imshow(np.squeeze(predict[0], axis=0))
  plt.show()
  
  # 테스트 이미지 분할 확인(원본)
  plt.figure(figsize=(12, 12))
  for idx, datapoint in enumerate(dataset['test'].take(3)):
      img = datapoint['image']
      mask = datapoint['segmentation_mask']
  
      img = tf.cast(img, tf.float32)
      img = img / 255.0
      mask -= 1
  
      plt.subplot(3, 3, idx*3+1)
      plt.imshow(img)
  
      plt.subplot(3, 3, idx*3+2)
      plt.imshow(np.squeeze(mask, axis=2))
  
      predict = tf.argmax(model.predict(tf.expand_dims(img, axis=0)), axis=-1)
      plt.subplot(3, 3, idx*3+3)
      plt.imshow(np.squeeze(predict[0], axis=0))
  plt.show()
  
  