Separate cars into train, test, and holdout sets from the original train set

F. Burkholder 25 Sep. 2017


In [1]:
import pandas as pd
import numpy as np
np.random.seed(1)

In [2]:
# data of interest is in train folder /train
import os 
from glob import glob

train_files = glob(os.path.join('train/', "*.jpg"))
train_ids_all = [s for s in train_files]

In [3]:
len(train_ids_all)


Out[3]:
5088

In [4]:
print(train_ids_all[0])
car_id = train_ids_all[0].split('_')[0].split('/')[1]
print(car_id)


train/d61b6bfeabb2_13.jpg
d61b6bfeabb2

In [5]:
car_ids = set([t.split('_')[0].split('/')[1] for t in train_ids_all])

In [6]:
len(car_ids)


Out[6]:
318

In [7]:
train_masks = glob(os.path.join('train_masks/', "*.gif"))
train_masks_all = [s for s in train_masks]

In [8]:
print(train_masks_all[0])
mask_id = train_masks_all[0].split('/')[1].split('_')[0]
print(mask_id)


train_masks/fc5f1a3a66cf_14_mask.gif
fc5f1a3a66cf

In [9]:
mask_ids = set([m.split('/')[1].split('_')[0] for m in train_masks_all])

In [10]:
len(mask_ids)


Out[10]:
318

In [11]:
car_ids - mask_ids


Out[11]:
set()

The same cars are in the train/ and train_masks/ folders. Good.


In [12]:
car_array = np.array(list(car_ids))

In [13]:
car_array


Out[13]:
array(['292f59c6a347', 'eb91b1c659a0', 'cf65b1c5e147', '858841907a4a',
       '2335eef27de6', '1ae8a68a40e4', 'f70052627830', '00087a6bd4dc',
       'd61b6bfeabb2', '917f262f1608', 'a070dbb32d2b', '5df60cf7cab2',
       '424658d9f222', '151c7f2d4183', 'dd70a0a51e3b', '2cb06c1f5bb1',
       'ba7e7421003b', 'fd9da5d0bb6f', '28d9a149cb02', 'f3eee6348205',
       'acb0fd30b83d', '4b74275babf7', 'bb7625a3f1d4', 'c53e374fdd72',
       '6c0cd487abcd', '344ece157efc', '7ac210ba75a1', '6e016b8b3617',
       '2af7c265531e', '9a2bfb24ba6e', 'eaf9eb0b2293', '04bd942b463b',
       '791c1a9775be', 'bd8d5780ed04', 'b44091aa62e4', '6d375bc2ece1',
       '8d78483ce302', '8b4b87ad7be6', 'bf9932f7aca8', '8d1a6723c458',
       '6bf969856536', 'd8e1c401ac3c', '42415ff65013', 'f00905abd3d7',
       '51f1cdf5f539', '4e7bc95552ed', '1c5748340af8', 'be86e79f1225',
       'ed8472086df8', '553c658b9efa', 'a7c1ef8d9cc9', '4e5ac4b9f074',
       'eeb7eeca738e', '23c088f6ec27', '0cdf5b5d0ce1', '3f3e362dea23',
       '32ff007d9505', '316bbfd3d93d', '3d7a1030deeb', '7251c8797749',
       'c502287be9f6', '2faf504842df', '7fbc761387f3', '69fba9e8f572',
       'cf89c3220bc4', 'd0dab700c896', 'eb07e3f63ad2', '878738468f6b',
       '2267f4aa0d2c', 'c4ac16c0dd59', 'b58cc561e560', 'a46baa0b3ac0',
       '630335e37f40', 'ae296a20fdd9', '85cba1a00135', '21a7e6726ee3',
       '6752de44b721', 'fff9b3a5373f', 'f3b482e091c0', 'eefc0d8c94f0',
       '3c6acfceb552', 'c52b272a5329', 'a4551c5409e3', '1a17a1bd648b',
       '4df1536de792', 'd9b2ded58b72', '1aba91a601c6', '90b65c521a8b',
       '364fd5fd7569', 'bb2f95262773', '32edfb7723ff', '9c2e82c23803',
       '0ee135a3cccc', 'cafee4122080', '304437bad7ff', '93881caf9f70',
       '880fec285c06', '4e308ad8a254', 'e8f607100c1f', '6d77433cc7ce',
       '4856fcb85be0', '3a7a8f03e713', 'fa613ac8eac5', '169f5f6d8c9d',
       'bf92e198d9b6', '768f299c36e1', '2ea62c1beee7', '99639c826ea5',
       '3f202616a2b9', 'de3ca5ec1e59', 'd46244bc42ed', '1e89e1af42e7',
       'e597d76a0c33', 'f8b6f4c39204', '61060ada97c9', 'a5fea424990e',
       'f4cd1286d5f4', 'ef5567efd904', '34b0c08544a5', '6a951d3a3131',
       'c87688f6960e', 'dc1c5c2b9e49', 'ce74dfdf123b', '78f63d808555',
       'f707d6fbc0cd', 'b1a27da4f01c', '2f39abb12388', 'c353e364e931',
       '7ab9747cf6d7', '2990f858e939', 'df642435a951', '78bfb9c780db',
       '82f72273d23e', 'ad5a6a058944', '4a4364d7fc6d', '0495dcf27283',
       'd3d0d2662918', '364923a5002f', '6ba36af67cb0', '6c3470c34408',
       '11acc40dc0ea', '0d3adbbc9a8b', '9ab2a45de8c7', '4bea0766c14c',
       '1e6f48393e17', '42a3297ccd4b', 'a2df64508b97', '11fcda0a9e1c',
       'c3614abc7405', '79a7691a90b1', '717689f3e788', '154ee2b6d27a',
       '98ee0624de87', 'dc995bf5296a', '8b88fb784951', '34defeec3ce6',
       '9dfaeb835626', '0789bed99cb8', 'cd2e588ef1c5', '66fea07e1152',
       '16f0ec26d608', 'dd47eb7ac4ee', '6ae670e86620', '6bff9e10288e',
       'de74550a306a', '9d8c465a0881', '2c57132da7a0', 'f98dbe8a5ee2',
       '5fb012d30fd9', '08a646be6b30', 'bad53871c7d9', 'c4f1840b2a65',
       '6dc8783eca68', '6cc98271f4dd', '5a194c1384f3', '898339fab87a',
       'e2230d0ceb02', 'e5fc2c1c4a8b', 'd8216041ffa9', 'fdc2c87853ce',
       '28109f18d9d4', '3071362591f7', '3789e15178c7', 'db09fec614d2',
       '87e334269396', '695f39dfac16', 'ed13cbcdd5d8', '3cb21125f126',
       '13cad6eeabe8', 'e114fcbb9723', '85bc655a7523', '70b6a79565fe',
       'e6892bf10d31', '293a0fa72e5b', '64f701f36437', '3810c5e1cdeb',
       '28d7fb5ba432', '3627b6f4c16d', '1952e5f91de0', '9274d0b3caa8',
       'ebfdf6ec7ede', '02159e548029', '683ddec95b82', '90fdd8932877',
       'd0392fd5feb6', 'c9acf5847ef1', '42b3feca1993', 'a1038b207299',
       '69915dab0755', 'ddb513df9406', '88bc32b9e1d9', '2a4a8964ebf3',
       'fc237174b128', '3c16409ab9db', 'c24b8293eceb', '0d1a9caf4350',
       '47ea8f25c948', '5bcde75955bb', '26ccab021981', 'a3e2e104d58a',
       '419687d50dcd', 'b98c63cd6102', '371f057d1242', 'b63a6f7952d4',
       '0ce66b539f52', '784ca55262c2', 'c8d52e0ba341', 'a078586ccdee',
       '5ab34f0e3ea5', '843763f47895', '48b3f0c936b5', '4dc8a7fe7c02',
       '03a857ce842d', '125cadcb9feb', 'f7ad86e13ed7', 'c3dafdb02e7f',
       'c7a94c46a3b2', '795d2ee07ce2', '888e04d96663', 'c7145a80089e',
       '0d53224da2b7', 'c2fd64144e97', '2cb91c2543fa', 'c9f382c7349e',
       'fa006be8b6d9', '6131a03dd028', 'd326cb5c3d12', '8603668953a6',
       '4fc2470c0f7b', 'efaef69e148d', '6343156803f9', '528eb34a4c29',
       '4baf50a3d8c2', '0ed6904e1004', 'feaf59172a01', 'e1d2c25f3510',
       '351c583eabd6', 'bf9a32258ef4', '120c1dc7b116', '54ee69d7d5e5',
       '31186febd775', '1258cf954183', '1ba84b81628e', '5bc30efeb7bf',
       'd8f11603ef08', '245f9c9eab8a', '3bca821c8c41', 'f1eb080c7182',
       '36a3f7f77e85', '781c5338fe02', '6f6dced498c4', 'b38c4e132183',
       '11f3dc041cfb', '5588758ee21c', 'fce0ba5b8ed7', '6d21aad248e4',
       'fb1b923dd978', '562706000bd7', 'fecea3036c59', 'fc5f1a3a66cf',
       '33a310ea7462', '3b45f83ba3fa', '3c54e71fd2c9', '4f1f065d78ac',
       'a7b9e343cf6b', '1adaeb44f109', 'b24fd9084449', 'f591b4f2e006',
       '7bd1142155ae', 'c6f50d44f141', 'bec93f7ddb19', '5d95d80e10a9',
       '86c10c8c6206', 'd1a3af34e674', '880ab8468df2', '68fcee2be01f',
       '430f0cb5666c', '9cc257b449d0', '4f0397cf7937', '0eeaf1ff136d',
       '3f8d611822bc', 'cf5add31c130', 'a56f923399ca', '1b25ea8ba94d',
       '6bfd601be25e', '40851e617b93', '42d81612e2ce', '8d5423cb763c',
       '1390696b70b6', '56023a17481b', '3afec4b5ac07', 'd22e6a2ca6df',
       'e05b96cc2b0d', '65051cfe0789', '189a2a32a615', '0de66245f268',
       '0795e132d090', '7abaa38c227f'], 
      dtype='<U12')

In [14]:
# Shuffle array so can easily separate array into train ids, test ids, and holdout ids
np.random.shuffle(car_array)

In [15]:
car_array


Out[15]:
array(['78f63d808555', 'd46244bc42ed', '3c6acfceb552', '78bfb9c780db',
       '82f72273d23e', '6131a03dd028', '5bcde75955bb', 'd9b2ded58b72',
       '08a646be6b30', '11f3dc041cfb', '0ee135a3cccc', '0ce66b539f52',
       '3d7a1030deeb', '364fd5fd7569', 'a7b9e343cf6b', '125cadcb9feb',
       '11fcda0a9e1c', '32edfb7723ff', 'cafee4122080', '6ba36af67cb0',
       'e5fc2c1c4a8b', 'd1a3af34e674', '2cb91c2543fa', '93881caf9f70',
       'ae296a20fdd9', 'd326cb5c3d12', '28d9a149cb02', 'e114fcbb9723',
       '5df60cf7cab2', '2a4a8964ebf3', 'ed13cbcdd5d8', '695f39dfac16',
       '7251c8797749', '0795e132d090', '54ee69d7d5e5', '843763f47895',
       '424658d9f222', '6e016b8b3617', '66fea07e1152', '7abaa38c227f',
       '9a2bfb24ba6e', '6ae670e86620', 'ce74dfdf123b', '0de66245f268',
       '6bff9e10288e', 'b98c63cd6102', '6c3470c34408', '1e89e1af42e7',
       'c87688f6960e', '351c583eabd6', '0ed6904e1004', 'b38c4e132183',
       '6343156803f9', '717689f3e788', 'dd47eb7ac4ee', 'ef5567efd904',
       '0d53224da2b7', '2335eef27de6', 'fdc2c87853ce', '5a194c1384f3',
       '4e5ac4b9f074', '784ca55262c2', '36a3f7f77e85', '917f262f1608',
       'fa613ac8eac5', 'ba7e7421003b', '292f59c6a347', 'c4f1840b2a65',
       '0eeaf1ff136d', '7fbc761387f3', '8b88fb784951', '6dc8783eca68',
       '6cc98271f4dd', 'b58cc561e560', 'bb2f95262773', 'd8e1c401ac3c',
       '3071362591f7', '87e334269396', '9d8c465a0881', 'a2df64508b97',
       '8d1a6723c458', 'e597d76a0c33', 'f3b482e091c0', '26ccab021981',
       '898339fab87a', '3f8d611822bc', 'fd9da5d0bb6f', '6a951d3a3131',
       '189a2a32a615', '5bc30efeb7bf', '42415ff65013', '02159e548029',
       '3bca821c8c41', 'fc237174b128', '3afec4b5ac07', '1ae8a68a40e4',
       'c353e364e931', 'bf9932f7aca8', '69915dab0755', 'f1eb080c7182',
       'b1a27da4f01c', '878738468f6b', '42b3feca1993', 'bf9a32258ef4',
       '768f299c36e1', '4856fcb85be0', 'b44091aa62e4', 'a1038b207299',
       '0789bed99cb8', 'de74550a306a', '5fb012d30fd9', 'a078586ccdee',
       '9cc257b449d0', '99639c826ea5', 'fc5f1a3a66cf', 'f3eee6348205',
       'd0392fd5feb6', 'dd70a0a51e3b', '9c2e82c23803', '48b3f0c936b5',
       '70b6a79565fe', '51f1cdf5f539', '3cb21125f126', '2ea62c1beee7',
       'cd2e588ef1c5', '5d95d80e10a9', 'f98dbe8a5ee2', '3f202616a2b9',
       '40851e617b93', 'eefc0d8c94f0', 'cf5add31c130', '28109f18d9d4',
       'd61b6bfeabb2', 'b63a6f7952d4', '6d77433cc7ce', 'fecea3036c59',
       '120c1dc7b116', '2af7c265531e', '04bd942b463b', '3f3e362dea23',
       '791c1a9775be', 'e6892bf10d31', 'ed8472086df8', '781c5338fe02',
       'fa006be8b6d9', 'bd8d5780ed04', '6d375bc2ece1', 'f7ad86e13ed7',
       '69fba9e8f572', 'efaef69e148d', '1c5748340af8', 'eb07e3f63ad2',
       '13cad6eeabe8', 'a56f923399ca', 'd22e6a2ca6df', '31186febd775',
       '5588758ee21c', 'be86e79f1225', '1b25ea8ba94d', 'c7a94c46a3b2',
       '6bf969856536', '4b74275babf7', '3a7a8f03e713', 'c4ac16c0dd59',
       '23c088f6ec27', '364923a5002f', '6c0cd487abcd', '1ba84b81628e',
       '4a4364d7fc6d', '03a857ce842d', 'f4cd1286d5f4', '6bfd601be25e',
       '9ab2a45de8c7', 'fce0ba5b8ed7', '42d81612e2ce', '1258cf954183',
       '32ff007d9505', '2faf504842df', '3627b6f4c16d', '9274d0b3caa8',
       '4df1536de792', '888e04d96663', '61060ada97c9', 'c6f50d44f141',
       '6f6dced498c4', '86c10c8c6206', '34b0c08544a5', '4dc8a7fe7c02',
       'bad53871c7d9', '42a3297ccd4b', 'e05b96cc2b0d', '562706000bd7',
       '0cdf5b5d0ce1', 'e2230d0ceb02', '1adaeb44f109', '64f701f36437',
       '8603668953a6', '371f057d1242', '3789e15178c7', '419687d50dcd',
       'e8f607100c1f', '5ab34f0e3ea5', 'df642435a951', '4e308ad8a254',
       '7bd1142155ae', 'a4551c5409e3', 'c24b8293eceb', 'c502287be9f6',
       '304437bad7ff', '293a0fa72e5b', '11acc40dc0ea', '16f0ec26d608',
       'c3614abc7405', '683ddec95b82', '98ee0624de87', 'f591b4f2e006',
       '0495dcf27283', 'c52b272a5329', 'f707d6fbc0cd', 'c53e374fdd72',
       'a070dbb32d2b', '151c7f2d4183', '880fec285c06', '88bc32b9e1d9',
       '316bbfd3d93d', '6d21aad248e4', '4e7bc95552ed', '1390696b70b6',
       '169f5f6d8c9d', '8d78483ce302', 'b24fd9084449', 'acb0fd30b83d',
       '8d5423cb763c', '4f0397cf7937', '21a7e6726ee3', 'ebfdf6ec7ede',
       '528eb34a4c29', '68fcee2be01f', '65051cfe0789', '79a7691a90b1',
       'a3e2e104d58a', '3c54e71fd2c9', 'cf65b1c5e147', '1952e5f91de0',
       '3c16409ab9db', 'eeb7eeca738e', 'f70052627830', 'c9acf5847ef1',
       '4fc2470c0f7b', '7ab9747cf6d7', '4f1f065d78ac', 'fff9b3a5373f',
       'f8b6f4c39204', 'd0dab700c896', 'db09fec614d2', '154ee2b6d27a',
       'bec93f7ddb19', '85bc655a7523', '85cba1a00135', 'c9f382c7349e',
       '90b65c521a8b', 'c7145a80089e', '4bea0766c14c', 'd3d0d2662918',
       '2c57132da7a0', '1a17a1bd648b', '34defeec3ce6', '2f39abb12388',
       '3810c5e1cdeb', '245f9c9eab8a', '880ab8468df2', 'bf92e198d9b6',
       'dc995bf5296a', 'c8d52e0ba341', '344ece157efc', '28d7fb5ba432',
       'cf89c3220bc4', '2cb06c1f5bb1', 'de3ca5ec1e59', '7ac210ba75a1',
       '6752de44b721', 'f00905abd3d7', '33a310ea7462', '858841907a4a',
       '553c658b9efa', '56023a17481b', 'eaf9eb0b2293', 'dc1c5c2b9e49',
       'a5fea424990e', '430f0cb5666c', '47ea8f25c948', 'd8f11603ef08',
       'ddb513df9406', 'eb91b1c659a0', 'bb7625a3f1d4', '00087a6bd4dc',
       '0d3adbbc9a8b', '1aba91a601c6', 'c2fd64144e97', '0d1a9caf4350',
       '2267f4aa0d2c', 'a7c1ef8d9cc9', '9dfaeb835626', '4baf50a3d8c2',
       'feaf59172a01', 'fb1b923dd978', 'd8216041ffa9', '3b45f83ba3fa',
       '795d2ee07ce2', 'a46baa0b3ac0', '2990f858e939', '1e6f48393e17',
       'ad5a6a058944', '90fdd8932877', 'e1d2c25f3510', '630335e37f40',
       'c3dafdb02e7f', '8b4b87ad7be6'], 
      dtype='<U12')

In [16]:
train_fract = 0.8
test_fract = 0.1
holdout_fract = 0.1
num_train = int(round(car_array.shape[0]*train_fract))
num_test = int(round(car_array.shape[0]*test_fract))
num_holdout = int(round(car_array.shape[0]*holdout_fract))
print("{0} train, {1} test, and {2} holdout cars.".format(num_train, num_test, num_holdout))
print("That's a total of {0} cars.".format(num_train + num_test + num_holdout))


254 train, 32 test, and 32 holdout cars.
That's a total of 318 cars.

In [17]:
ids_train = car_array[:num_train]
ids_test = car_array[num_train:num_train + num_test]
ids_holdout = car_array[num_train + num_test:]
out = "There are {0} in train, {1} in test, and {2} in the holdout"
print(out.format(ids_train.shape[0], ids_test.shape[0], ids_holdout.shape[0]))


There are 254 in train, 32 in test, and 32 in the holdout

In [18]:
np.savetxt('ids_train.txt', ids_train, fmt='%s', delimiter='\n') 
np.savetxt('ids_test.txt', ids_test, fmt='%s', delimiter='\n')
np.savetxt('ids_holdout.txt', ids_holdout, fmt='%s', delimiter='\n')

In [ ]: