imdb.py 1.1 KB
Newer Older
D
dangqingqing 已提交
1 2 3 4 5 6
from __future__ import print_function
import six.moves.cPickle as pickle
import gzip
import os
import numpy

7

D
dangqingqing 已提交
8 9 10 11 12 13 14 15 16
def get_dataset_file(dataset, default_dataset, origin):
    data_dir, data_file = os.path.split(dataset)
    if (not os.path.isfile(dataset)) and data_file == default_dataset:
        from six.moves import urllib
        print('Downloading data from %s' % origin)
        urllib.request.urlretrieve(origin, dataset)

    return dataset

17

D
dangqingqing 已提交
18 19 20 21 22 23
def create_data(path="imdb.pkl"):

    if (not os.path.isfile('imdb.train.pkl')):
        path = get_dataset_file(
            path, "imdb.pkl",
            "http://www.iro.umontreal.ca/~lisa/deep/data/imdb.pkl")
24

D
dangqingqing 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
        if path.endswith(".gz"):
            f = gzip.open(path, 'rb')
        else:
            f = open(path, 'rb')

        train_set = pickle.load(f)
        test_set = pickle.load(f)
        f.close()

        pickle.dump(train_set, open('imdb.train.pkl', 'wb'))
        pickle.dump(test_set, open('imdb.test.pkl', 'wb'))

    if (not os.path.isfile('train.list')):
        file('train.list', 'w').write('imdb.train.pkl\n')

40

D
dangqingqing 已提交
41 42 43
def main():
    create_data('imdb.pkl')

44

D
dangqingqing 已提交
45 46
if __name__ == "__main__":
    main()