def data_loader(cfg, fname): dims = cfg['dims'] chunk_size = cfg['batch_size'] * cfg['batches_per_chunk'] xc = np.zeros(( chunk_size, cfg['n_channels'], ) + dims, dtype=np.float32) reader = npytar.NpyTarReader(fname) yc = [] for ix, (x, name) in enumerate(reader): cix = ix % chunk_size xc[cix] = x.astype(np.float32) yc.append(int(name.split('.')[0]) - 1) if len(yc) == chunk_size: xc = jitter_chunk(xc, cfg) yield (2.0 * xc - 1.0, np.asarray(yc, dtype=np.float32)) yc = [] xc.fill(0) if len(yc) > 0: # pad to nearest multiple of batch_size if len(yc) % cfg['batch_size'] != 0: new_size = int(np.ceil( len(yc) / float(cfg['batch_size']))) * cfg['batch_size'] xc = xc[:new_size] xc[len(yc):] = xc[:(new_size - len(yc))] yc = yc + yc[:(new_size - len(yc))] xc = jitter_chunk(xc, cfg) yield (2.0 * xc - 1.0, np.asarray(yc, dtype=np.float32))
def data_loader(cfg, fname): dims = cfg['dims'] chunk_size = cfg['n_rotations'] xc = np.zeros((chunk_size, cfg['n_channels'],)+dims, dtype=np.float32) reader = npytar.NpyTarReader(fname) yc = [] for ix, (x, name) in enumerate(reader): cix = ix % chunk_size xc[cix] = x.astype(np.float32) yc.append(int(name.split('.')[0])-1) if len(yc) == chunk_size: yield (2.0*xc - 1.0, np.asarray(yc, dtype=np.float32)) yc = [] xc.fill(0) assert(len(yc)==0)
def data_loader(fname): # the number for reading each time chunk_size = cfg['batch_size']*cfg['batches_per_chunk'] xc = np.zeros((chunk_size, cfg['n_channels'],cfg['n_levels'],cfg['n_rings'],dims), dtype=np.float32) reader = npytar.NpyTarReader(fname) yc = [] for ix, (x, name) in enumerate(reader): cix = ix % chunk_size xc[cix] = x.astype(np.float32) yc.append(int(name.split('.')[0])-1) if len(yc) == chunk_size: yield (xc, np.asarray(yc, dtype=np.float32)) yc = [] xc.fill(0) if len(yc) > 0: # pad to nearest multiple of batch_size new_size = int(np.ceil(len(yc)/float(cfg['batch_size'])))*cfg['batch_size'] xc = xc[:new_size] xc[len(yc):] = xc[:(new_size-len(yc))] yc = yc + yc[:(new_size - len(yc))] yield (xc, np.asarray(yc, dtype=np.float32))
def main(args): reader = npytar.NpyTarReader(args.viz_data_fname) out = np.load(args.viz_out_fname) yhat = out['yhat'] ygnd = out['ygnd'] css = """ html { margin: 0 } body { background:#fff; color:#000; font:75%/1.5em Helvetica, "DejaVu Sans", "Liberation sans", "Bitstream Vera Sans", sans-serif; position:relative; } /*dt { font-weight: bold; float: left; clear; left }*/ div { padding: 10px; width: 80%; margin: auto } img { border: 1px solid #eee } dl { margin:0 0 1.5em; } dt { font-weight:700; } dd { margin-left:1.5em; } table { border-collapse:collapse; border-spacing:0; margin:0 0 1.5em; padding:0; } td { padding:0.333em; vertical-align:middle; } }""" with open(args.viz_fname, 'w') as f: f.write('<html><head><style>') f.write(css) f.write('</style></head>') f.write('<body>') display_ix = np.random.randint(0, len(ygnd), args.num_instances) xds, yds = [], [] for ix, (xd, yd) in enumerate(reader): if ix in display_ix: dix = ix iloc = np.round(np.array(xd.shape) / 2) imgXY = show_png(xd[:, :, iloc[2]]).get_html_embed_code() imgXZ = show_png(xd[:, iloc[1], :]).get_html_embed_code() imgYZ = show_png(xd[iloc[0], :, :]).get_html_embed_code() f.write('<div>') f.write('<table><tr><td>') f.write(imgXY) f.write(imgXZ) f.write(imgYZ) f.write('</td>') f.write('<td>') f.write('<dl><dt>Instance:</dt><dd>{}</dd>'.format(yd)) f.write('<dt>Predicted label:</dt><dd>{}</dd>'.format( str(yhat[dix]))) f.write('<dt>True label:</dt><dd>{}</dd></dl>'.format( str(ygnd[dix]))) f.write('</td></tr></table>') f.write('</div>') xds.append(xd) yds.append(yd) f.write('</body></html>')
from path import Path import numpy as np import voxnet from voxnet import npytar from voxnet import isovox from voxnet.data import shapenet10 parser = argparse.ArgumentParser() parser.add_argument('out_fname', type=Path) parser.add_argument('test_fname', type=Path) parser.add_argument('viz_fname', type=Path) parser.add_argument('--num-instances', type=int, default=10) args = parser.parse_args() reader = npytar.NpyTarReader(args.test_fname) out = np.load(args.out_fname) yhat = out['yhat'] ygnd = out['ygnd'] iv = isovox.IsoVox() css = """ html { margin: 0 } body { background:#fff; color:#000; font:75%/1.5em Helvetica, "DejaVu Sans", "Liberation sans", "Bitstream Vera Sans", sans-serif; position:relative; }
xc[cix] = x.astype(np.float32) yc.append(int(name.split('.')[0]) - 1) if ix % num_divide == 0: xc = jitter_chunk(xc) xc = 2.0 * xc - 1.0 xc = np.array(xc) xc = np.reshape(xc, (-1, 32, 32, 32)) data_list.append(xc) xc.fill(0) return data_list, yc if Train: # Read all dataset reader = npytar.NpyTarReader('shapenet40_train.tar') # Set subset for augmentation xc = np.zeros((num_train_divide, 32, 32, 32)) # After augmentation # data_list & yc includes whole dataset data_list, yc = tar_reader(reader, xc, Train) yc = np.array(yc) print('Start Saving!') num_h5 = 9 len_train = int(num_train / num_train_divide) # 36 data_list_divide = int(len_train / num_h5) # 3 label_divide = int(num_train / num_h5) # 9843 for i in range(num_h5): final_xc = np.stack(data_list[data_list_divide * i:data_list_divide *