Skip to content

Commit

Permalink
Merge pull request microsoft#28 from stonebig/patch-2
Browse files Browse the repository at this point in the history
python 3 compatibility fix
  • Loading branch information
chrisbasoglu committed Jan 26, 2016
2 parents c08b105 + cafbf2d commit 9177216
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions Examples/Image/Miscellaneous/CIFAR-10/CIFAR_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,33 +38,33 @@ def readBatch(src, outFmt):
return np.hstack((np.reshape(d['labels'], (len(d['labels']), 1)), feat))

def loadData(src, outFmt):
print 'Downloading ' + src
print ('Downloading ' + src)
fname, h = urllib.urlretrieve(src, './delete.me')
print 'Done.'
print ('Done.')
try:
print 'Extracting files...'
print ('Extracting files...')
with tarfile.open(fname) as tar:
tar.extractall()
print 'Done.'
print 'Preparing train set...'
print ('Done.')
print ('Preparing train set...')
trn = np.empty((0, NumFeat + 1))
for i in range(5):
batchName = './cifar-10-batches-py/data_batch_{0}'.format(i + 1)
trn = np.vstack((trn, readBatch(batchName, outFmt)))
print 'Done.'
print 'Preparing test set...'
print ('Done.')
print ('Preparing test set...')
tst = readBatch('./cifar-10-batches-py/test_batch', outFmt)
print 'Done.'
print ('Done.')
finally:
os.remove(fname)
return (trn, tst)

def usage():
print 'Usage: CIFAR_convert.py [-f <format>] \n where format can be either cudnn or legacy. Default is cudnn.'
print ('Usage: CIFAR_convert.py [-f <format>] \n where format can be either cudnn or legacy. Default is cudnn.')

def parseCmdOpt(argv):
if len(argv) == 0:
print "Using cudnn output format."
print ("Using cudnn output format.")
return "cudnn"
try:
opts, args = getopt.getopt(argv, 'hf:', ['help', 'outFormat='])
Expand All @@ -78,17 +78,17 @@ def parseCmdOpt(argv):
elif opt in ('-f', '--outFormat'):
fmt = arg
if fmt != 'cudnn' and fmt != 'legacy':
print 'Invalid output format option.'
print ('Invalid output format option.')
usage()
sys.exit(1)
return fmt

if __name__ == "__main__":
fmt = parseCmdOpt(sys.argv[1:])
trn, tst = loadData('http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz', fmt)
print 'Writing train text file...'
print ('Writing train text file...')
np.savetxt(r'./Train.txt', trn, fmt = '%u', delimiter='\t')
print 'Done.'
print 'Writing test text file...'
print ('Done.')
print ('Writing test text file...')
np.savetxt(r'./Test.txt', tst, fmt = '%u', delimiter='\t')
print 'Done.'
print ('Done.')

0 comments on commit 9177216

Please sign in to comment.