티스토리 뷰

728x90
반응형

https://github.com/WegraLee/deep-learning-from-scratch

 

 

GitHub - WegraLee/deep-learning-from-scratch: 『밑바닥부터 시작하는 딥러닝』(한빛미디어, 2017)

『밑바닥부터 시작하는 딥러닝』(한빛미디어, 2017). Contribute to WegraLee/deep-learning-from-scratch development by creating an account on GitHub.

github.com

 

Ch3장. 신경망 (97페이지) 

load_mnist(flatten = True, normalize = False) 오류 

import sys, os
import numpy as np
sys.path.append(os.pardir)  # 부모 디렉터리의 파일을 가져올 수 있도록 설정
import dataset.mnist as mnist
from mnist import load_mnist #같은 디렉토리에 있는 mnist.py 파일을 import

(x_train, t_train), (x_test, t_test) = load_mnist(flatten = True, normalize = False)

print(x_train.shape)
print(t_train.shape)
print(x_test.shape)
print(t_test.shape)

 

위의 코드를 수행하면 오류가 난다... 

URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1000)>

 

 

 

문제의 원인이 되는 부분을 찾아보려고 load_mnist 함수를 뒤져보면 원인이 되는 부분은 아래와 같다.

 

 

 

이부분이 왜 문제냐면....

 

 

http://yann.lecun.com/exdb/mnist/

http://yann.lecun.com/exdb/mnist/

 

MNIST handwritten digit database, Yann LeCun, Corinna Cortes and Chris Burges

 

yann.lecun.com

결론 부터 이야기하면.....위의 사이트에서 더이상 gz 파일을 제공하지 않기 때문이다. 

 

에이 설마, 하면서 저도 들어가 봤습니다.....

 

 

일단 들어갈 때부터 인증서가 오류가.....매우 의심스러운데...
그래도 일단 끝까지 들어가서 파일을 다운을 받아보기로 하였다. 

 

 

 

못믿는 사람들을 위한 최종 경로 : 심심하면 눌러보세요....
https://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz

 

 

 

 

최종 다운로드 경로에서
결국돌아오는건 443에러 뿐....

 

 

 

 

 

그럼 어떻게 하면 수행할 수 있을까?




 

<여기서 부터는 해결 방법 >

 

결국 책에서 얘기하는 것은 해당 pkl 파일과 gz 파일들을 한군데에 몰아 두면 학습을 할 수 있게 구성이 되어 있다.

그래서 해당 파일을 찾아서 특정 경로를 지정해서 다운받고 경로를 맞춰 두었다.

 

해당 파일들은 아래의 github에 링크로 달아 두었으니 다운 받아 가시면 됩니다.

https://github.com/4-OurFuture/DeepLearning_from_scratch/tree/main/ch3/dataset 

 

DeepLearning_from_scratch/ch3/dataset at main · 4-OurFuture/DeepLearning_from_scratch

DeepLearning_from_scratch. Contribute to 4-OurFuture/DeepLearning_from_scratch development by creating an account on GitHub.

github.com

 

 

진짜 중요한 mnist.pkl 파일인데 해당 파일은 25MB가 넘어서 올릴 수가 없네요. 

mnist.pkl 파일을 tensorflow 에서 다운 받아서 강제로 pkl 파일로 바꾸는 법은 아래와 같습니다.

다행히 mnist 파일은 tensorflow에서 여전히 다운 받을 수 있지만 pkl 형태가 아닌 npz(넘파이 압축파일) 형태로 지원을 하고 있어서 변환이 필요합니다.

 

아래의 코드로 다운 받고 해당 파일을 통해서 수행을 하면 됩니다. 

import pickle
import numpy as np
import tensorflow as tf

# TensorFlow로 MNIST 데이터셋 다운로드
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

# 데이터셋을 딕셔너리로 저장
mnist_data = {
    "x_train": x_train,
    "y_train": y_train,
    "x_test": x_test,
    "y_test": y_test
}

# pickle로 저장
with open("mnist.pkl", "wb") as f:
    pickle.dump(mnist_data, f)

 

 

 

실제 구동하는 법: mnist.py 파일에서 dataset폴더를 지정 (책에 나와있는 대로 폴더 구조를 가져갔으면 안하셔도 됩니다.) 

 

출력결과

--> 책에서 말한 대로 각 데이터의 shape이 정상적으로 출력 되는 것 확인 완료 

 

 

 

혹시 그래도 막히는게 있으면, 댓글로 알려주시면 도움을 드릴 수 있으면 찾아 드릴께요 😊

오늘보다 더나은 내일을 위해서!!

 

728x90
반응형