본문 바로가기
책&강의 학습/올인원 패키지 :딥러닝&인공지능

[패스트캠퍼스 수강 후기] 인공지능강의 100% 환급 챌린지 20회차 미션

by 소한보 2020. 7. 18.

Part3-26. Post Process - 01. 결과에 대한 개념 이해 (Kaggle 하기) - 1

데이터프레임을 읽은다음에 학습시키는 방법으로 진행

csv_path = '../input/aerial-cactus-identification/train.csv'
df = pd.read_csv(csv_path)

파일위치 확득

join을 사용해서 path를 획득

file_paths = [os.path.join(train_dir, fname) for fname in filenames]
file_paths[:5]
>>> ['../input/aerial-cactus-identification/train/train/0004be2cfeaba1c0361d39e2b000257b.jpg',
 '../input/aerial-cactus-identification/train/train/000c8a36845c0208e833c79c1bffedd1.jpg',
 '../input/aerial-cactus-identification/train/train/000d1e9a533f62e55c289303b072733d.jpg',
 '../input/aerial-cactus-identification/train/train/0011485b40695e9138e92d0b3fb55128.jpg',
 '../input/aerial-cactus-identification/train/train/0014d7a11e90b62848904c1418fc8cf2.jpg']

sample submission 확인

확률만 부여되어있음

sample_csv_path = '../input/aerial-cactus-identification/sample_submission.csv'
sample_df = pd.read_csv(sample_csv_path)
sample_df.head()
>>> id    has_cactus
0    000940378805c44108d287872b2f04ce.jpg    0.5
1    0017242f54ececa4512b4d7937d1e21e.jpg    0.5
2    001ee6d8564003107853118ab87df407.jpg    0.5
3    002e175c3c1e060769475f52182583d0.jpg    0.5
4    0036e44a7e8f7218e9bc7bf8137e4943.jpg    0.5

train셋에서 test용도 분리

test.csv에는 답이 없기때문에 학습용으로 분리

train_df = train_df[:-500]
test_df = train_df[-500:]

len(train_df), len(test_df)

Data 1개로 샘플

path = train_df['id'][0]

Data Explore

train_df.head()

img_pil = Image.open(path)
image = np.array(img_pil)

image.shape

plt.imshow(image)
plt.show()

hyperparameter

input_shape = (32, 32, 3)
batch_size = 32
num_classes = 2
num_epochs = 10

learning_rate = 0.01

model

inputs = layers.Input(input_shape)
net = layers.Conv2D(64, (3, 3), padding='same')(inputs)
net = layers.Conv2D(64, (3, 3), padding='same')(net)
net = layers.Conv2D(64, (3, 3), padding='same')(net)
net = layers.BatchNormalization()(net)
net = layers.Activation('relu')(net)
net = layers.MaxPooling2D(pool_size=(2, 2))(net)

net = layers.Conv2D(128, (3, 3), padding='same')(net)
net = layers.Conv2D(128, (3, 3), padding='same')(net)
net = layers.Conv2D(128, (3, 3), padding='same')(net)
net = layers.BatchNormalization()(net)
net = layers.Activation('relu')(net)
net = layers.MaxPooling2D(pool_size=(2, 2))(net)
net = layers.Dropout(0.25)(net)

net = layers.Conv2D(256, (3, 3), padding='same')(net)
net = layers.Conv2D(256, (3, 3), padding='same')(net)
net = layers.Conv2D(256, (3, 3), padding='same')(net)
net = layers.BatchNormalization()(net)
net = layers.Activation('relu')(net)
net = layers.MaxPooling2D(pool_size=(2, 2))(net)
net = layers.Dropout(0.25)(net)

net = layers.Conv2D(512, (3, 3), padding='same')(net)
net = layers.Conv2D(512, (3, 3), padding='same')(net)
net = layers.Conv2D(512, (3, 3), padding='same')(net)
net = layers.BatchNormalization()(net)
net = layers.Activation('relu')(net)
net = layers.MaxPooling2D(pool_size=(2, 2))(net)
net = layers.Dropout(0.25)(net)

net = layers.Conv2D(512, (3, 3), padding='same')(net)
net = layers.Conv2D(512, (3, 3), padding='same')(net)
net = layers.Conv2D(512, (3, 3), padding='same')(net)
net = layers.BatchNormalization()(net)
net = layers.Activation('relu')(net)
net = layers.MaxPooling2D(pool_size=(2, 2))(net)
net = layers.Dropout(0.25)(net)

net = layers.Flatten()(net)
net = layers.Dense(512)(net)
net = layers.Activation('relu')(net)
net = layers.Dropout(0.5)(net)
net = layers.Dense(num_classes)(net)
net = layers.Activation('softmax')(net)

model = tf.keras.Model(inputs=inputs, outputs=net)

data preprocess

앞에서 했던거 가져옴
augmentaion 진행

train_datagen = ImageDataGenerator(
    rescale=1./255.,
    width_shift_range=0.3,
    zoom_range=0.2,
    horizontal_flip=True
)

test_datagen = ImageDataGenerator(
    rescale=1./255.
)

train_generator = train_datagen.flow_from_dataframe(
    train_df,
    x_col='id',
    y_col='has_cactus',
    target_size=input_shape[:2],
    batch_size=batch_size,
    class_mode='sparse' #binary로 해도 가능 0,1 이라서 
)

test_generator = test_datagen.flow_from_dataframe(
    test_df,
    x_col='id',
    y_col='has_cactus',
    target_size=input_shape[:2],
    batch_size=batch_size,
    class_mode='sparse'
)

train

epoch를 늘리거나 lr를 늘려봐도 됨

model.fit_generator(
    train_generator,
    steps_per_epoch=len(train_generator),
    epochs=num_epochs,
    validation_data=test_generator,
    validation_steps=len(test_generator)
)

Evaluate

파일을 하나씩 예측하고 받아서 제출

 # test용 path
path = os.path.join(test_dir, sample_df['id'][0])

img_pil = Image.open(path)
image = np.array(img_pil)
image.shape
>>> (32,32,3)

plt.imshow(image)
plt.show()

pred = model.predict(image[tf.newaxis, ...])
pred
>>> array([[0.,1.]], dtype=float32)
pred = np.argmax(pred)
pred
>>>
1

위 방법 전체로 진행

for문으로 돌려서 하나씩 만들기 진행
tqdm_notebook으로 진행사항 확인

preds = []

for fname in tqdm_notebook(sample_df['id']):
    path = os.path.join(test_dir, fname)

    img_pil = Image.open(path)
    image = np.array(img_pil)

    pred = model.predict(image[tf.newaxis, ...])
    pred = np.argmax(pred)
    preds.append(pred)

제출

csv로 저장해서 제출
에러날수 있기 때문에 notebook 다운받는거 추천
commit하는 게 오래걸림
submit to commpition으로 제출
대회가 끝나서 다소 아쉽

submission_df = pd.DataFrame(data={'id': sample_df['id'], 'has_cactus': preds})
submission_df.to_csv('samplesubmission.csv', index=False)

딥러닝/인공지능 올인원 패키지 Online

댓글