def get_comma_separated_data(raw):
# Convert to long string
header, data = "".join(raw).strip().split(" = ")
# Remove trailing comma
assert data[-1] == ';'
data = data[:-1]
# Remove newline characters and convert to list
data = eval(data.replace("\n", ''))
shape = tuple(eval(header[header.index("["):header.index("]") + 1]))
step_size = functools.reduce(operator.mul, shape) + 1
years = np.array(data[::step_size], dtype=int)
data = np.stack([
np.array(data[1 + index * step_size:(index + 1) * step_size]).reshape(shape)
for index in range(len(years))
], axis=-1)
return header, years, data
python类stack()的实例源码
def get_3d_data_slices(slices): # get data in Hunsfield Units
slices.sort(key = lambda x: float(x.ImagePositionPatient[2])) # from v 9
image = np.stack([s.pixel_array for s in slices])
image = image.astype(np.int16) # ensure int16 (it may be here uint16 for some images )
image[image == -2000] = 0 #correcting cyindrical bound entrioes to 0
# Convert to Hounsfield units (HU)
# The intercept is usually -1024
for slice_number in range(len(slices)): # from v 8
intercept = slices[slice_number].RescaleIntercept
slope = slices[slice_number].RescaleSlope
if slope != 1: # added 16 Jan 2016, evening
image[slice_number] = slope * image[slice_number].astype(np.float64)
image[slice_number] = image[slice_number].astype(np.int16)
image[slice_number] += np.int16(intercept)
return np.array(image, dtype=np.int16)
def get_pixels_hu(slices):
image = np.stack([s.pixel_array for s in slices])
image = image.astype(np.int16)
# Set outside-of-scan pixels to 0
# The intercept is usually -1024, so air is approximately 0
image[image == -2000] = 0
# Convert to Hounsfield units (HU)
### slope can differ per slice -- so do it individually (case in point black_tset, slices 95 vs 96)
### Changes/correction - 31.01.2017
for slice_number in range(len(slices)):
intercept = slices[slice_number].RescaleIntercept
slope = slices[slice_number].RescaleSlope
if slope != 1:
image[slice_number] = slope * image[slice_number].astype(np.float64)
image[slice_number] = image[slice_number].astype(np.int16)
image[slice_number] += np.int16(intercept)
return np.array(image, dtype=np.int16)
def get_3d_data_slices(slices): # get data in Hunsfield Units
#slices = [dicom.read_file(path + '/' + s) for s in os.listdir(path)]
#slices.sort(key=lambda x: int(x.InstanceNumber)) # was x.InstanceNumber
slices.sort(key = lambda x: int(x.ImagePositionPatient[2])) # from v 8
image = np.stack([s.pixel_array for s in slices])
image = image.astype(np.int16) # ensure int16 (it may be here uint16 for some images )
image[image == -2000] = 0 #correcting cyindrical bound entrioes to 0
# Convert to Hounsfield units (HU)
# The intercept is usually -1024
for slice_number in range(len(slices)): # from v 8
intercept = slices[slice_number].RescaleIntercept
slope = slices[slice_number].RescaleSlope
if slope != 1: # added 16 Jan 2016, evening
image[slice_number] = slope * image[slice_number].astype(np.float64)
image[slice_number] = image[slice_number].astype(np.int16)
image[slice_number] += np.int16(intercept)
return np.array(image, dtype=np.int16)
def get_3d_data_hu(path): # get data in Hunsfield Units
slices = [dicom.read_file(path + '/' + s) for s in os.listdir(path)]
#slices.sort(key=lambda x: int(x.InstanceNumber)) # was x.InstanceNumber
#slices.sort(key = lambda x: int(x.ImagePositionPatient[2])) # from v8 - BUGGY
slices.sort(key = lambda x: float(x.ImagePositionPatient[2])) # from 22.02
image = np.stack([s.pixel_array for s in slices])
image = image.astype(np.int16) # ensure int16 (it may be here uint16 for some images )
image[image == -2000] = 0 #correcting cyindrical bound entrioes to 0
# Convert to Hounsfield units (HU)
# The intercept is usually -1024
for slice_number in range(len(slices)): # from v 8
intercept = slices[slice_number].RescaleIntercept
slope = slices[slice_number].RescaleSlope
if slope != 1: # added 16 Jan 2016, evening
image[slice_number] = slope * image[slice_number].astype(np.float64)
image[slice_number] = image[slice_number].astype(np.int16)
image[slice_number] += np.int16(intercept)
return np.array(image, dtype=np.int16)
def test_against_numpy_nanstd(self):
source = [np.random.random((16, 12, 5)) for _ in range(10)]
for arr in source:
arr[randint(0, 15), randint(0, 11), randint(0, 4)] = np.nan
stack = np.stack(source, axis = -1)
for axis in (0, 1, 2, None):
for ddof in range(4):
with self.subTest('axis = {}, ddof = {}'.format(axis, ddof)):
from_numpy = np.nanstd(stack, axis = axis, ddof = ddof)
from_ivar = last(istd(source, axis = axis, ddof = ddof, ignore_nan = True))
self.assertSequenceEqual(from_numpy.shape, from_ivar.shape)
self.assertTrue(np.allclose(from_ivar, from_numpy))
def test_against_numpy(self):
""" Test iall against numpy.all """
stream = [np.zeros((8, 16, 2)) for _ in range(11)]
stream[3][3,0,1] = 1 # so that np.all(axis = None) evaluates to False
stack = np.stack(stream, axis = -1)
with self.subTest('axis = None'):
from_numpy = np.all(stack, axis = None)
from_stream = last(iall(stream, axis = None))
self.assertEqual(from_numpy, from_stream)
for axis in range(stack.ndim):
with self.subTest('axis = {}'.format(axis)):
from_numpy = np.all(stack, axis = axis)
from_stream = last(iall(stream, axis = axis))
self.assertTrue(np.allclose(from_numpy, from_stream))
def snapshot_to_xarray_variable(self, key, clock=None):
"""Convert snapshots taken for a specific model variable to an
xarray.Variable object.
"""
proc_name, var_name = key
variable = self.model._processes[proc_name]._variables[var_name]
array_list = self.snapshot_values[key]
first_array = array_list[0]
if len(array_list) == 1:
data = first_array
else:
data = np.stack(array_list)
dims = _get_dims_from_variable(first_array, variable)
if clock is not None and len(array_list) > 1:
dims = (clock,) + dims
attrs = variable.attrs.copy()
attrs['description'] = variable.description
return xr.Variable(dims, data, attrs=attrs)
def get_interv_table(model,intrv=True):
n_batches=25
table_outputs=[]
d_vals=np.linspace(TINY,0.6,n_batches)
for name in model.cc.node_names:
outputs=[]
for d_val in d_vals:
do_dict={model.cc.node_dict[name].label_logit : d_val*np.ones((model.batch_size,1))}
outputs.append(model.sess.run(model.fake_labels,do_dict))
out=np.vstack(outputs)
table_outputs.append(out)
table=np.stack(table_outputs,axis=2)
np.mean(np.round(table),axis=0)
return table
#dT=pd.DataFrame(index=p_names, data=T, columns=do_names)
#T=np.mean(np.round(table),axis=0)
#table=get_interv_table(model)
rl-network-test.py 文件源码
项目:Deep-Learning-with-Keras
作者: PacktPublishing
项目源码
文件源码
阅读 25
收藏 0
点赞 0
评论 0
def preprocess_images(images):
if images.shape[0] < 4:
# single image
x_t = images[0]
x_t = imresize(x_t, (80, 80))
x_t = x_t.astype("float")
x_t /= 255.0
s_t = np.stack((x_t, x_t, x_t, x_t), axis=2)
else:
# 4 images
xt_list = []
for i in range(images.shape[0]):
x_t = imresize(images[i], (80, 80))
x_t = x_t.astype("float")
x_t /= 255.0
xt_list.append(x_t)
s_t = np.stack((xt_list[0], xt_list[1], xt_list[2], xt_list[3]),
axis=2)
s_t = np.expand_dims(s_t, axis=0)
return s_t
############################# main ###############################
rl-network-train.py 文件源码
项目:Deep-Learning-with-Keras
作者: PacktPublishing
项目源码
文件源码
阅读 30
收藏 0
点赞 0
评论 0
def preprocess_images(images):
if images.shape[0] < 4:
# single image
x_t = images[0]
x_t = imresize(x_t, (80, 80))
x_t = x_t.astype("float")
x_t /= 255.0
s_t = np.stack((x_t, x_t, x_t, x_t), axis=2)
else:
# 4 images
xt_list = []
for i in range(images.shape[0]):
x_t = imresize(images[i], (80, 80))
x_t = x_t.astype("float")
x_t /= 255.0
xt_list.append(x_t)
s_t = np.stack((xt_list[0], xt_list[1], xt_list[2], xt_list[3]),
axis=2)
s_t = np.expand_dims(s_t, axis=0)
return s_t
def calc_metrics(self, data_gen, history, dataset, logs):
y_true = []
predictions = []
for i in range(data_gen.steps):
if self.verbose == 1:
print "\r\tdone {}/{}".format(i, data_gen.steps),
(x,y) = next(data_gen)
pred = self.model.predict(x, batch_size=self.batch_size)
if isinstance(x, list) and len(x) == 2: # deep supervision
for m, t, p in zip(x[1].flatten(), y.flatten(), pred.flatten()):
if np.equal(m, 1):
y_true.append(t)
predictions.append(p)
else:
y_true += list(y.flatten())
predictions += list(pred.flatten())
print "\n"
predictions = np.array(predictions)
predictions = np.stack([1-predictions, predictions], axis=1)
ret = metrics.print_metrics_binary(y_true, predictions)
for k, v in ret.iteritems():
logs[dataset + '_' + k] = v
history.append(ret)
def calc_metrics(self, data, history, dataset, logs):
y_true = []
predictions = []
B = self.batch_size
for i in range(0, len(data[0]), B):
if self.verbose == 1:
print "\r\tdone {}/{}".format(i, len(data[0])),
(x,y) = (data[0][i:i+B], data[1][i:i+B])
outputs = self.model.predict(x, batch_size=B)
if isinstance(y[0], list): # target replication
y_true += list(y[0].flatten())
predictions += list(outputs[0].flatten())
else:
y_true += list(np.array(y).flatten())
predictions += list(outputs.flatten())
print "\n"
predictions = np.array(predictions)
predictions = np.stack([1-predictions, predictions], axis=1)
ret = metrics.print_metrics_binary(y_true, predictions)
for k, v in ret.iteritems():
logs[dataset + '_' + k] = v
history.append(ret)
def get_document_batch(self, doc_id):
"""builds batch of all mention pairs in one document
Args:
doc_id: id of document
Returns:
feature representation of mentions and labels
"""
mentions = self.dl.get_all_mentions_from_doc(doc_id)
if len(mentions) == 0:
return None, None
A, B = [], []
for a in mentions:
for b in mentions:
A.append(a)
B.append(b)
A_f = [self._mention_to_features(m) for m in A]
B_f = [self._mention_to_features(m) for m in B]
AB_f = self._pair_features(A, B)
A = [self.dl.mention_features[m] for m in A]
B = [self.dl.mention_features[m] for m in B]
return np.vstack(A), np.stack(A_f), np.vstack(B), np.stack(B_f), np.stack(AB_f)
def get_space_separated_data(raw):
assert raw[0].strip().endswith("= [")
assert raw[-1].strip().endswith("];")
header = raw[0].replace("= [", "").strip()
shape = tuple(eval(header[header.index("["):header.index("]") + 1]))
data = [eval(line.strip().replace(" ", ",")) for line in raw[1:-1]]
if len(shape) == 1:
step_size = 1
else:
step_size = functools.reduce(operator.mul, shape[:-1])
years = np.array(data[::step_size + 1], dtype=int)
subarrays = [
np.array(data[index * (step_size + 1) + 1:(index + 1) * (step_size + 1)]).reshape(shape)
for index in range(len(years))
]
return header, years, np.stack(subarrays, axis=-1)
def generate(self):
for pid in self.id2candidates_path.iterkeys():
patient_path = self.id2patient_path[pid]
print pid, patient_path
img, pixel_spacing = utils_lung.read_dicom_scan(patient_path)
print self.id2candidates_path[pid]
candidates = utils.load_pkl(self.id2candidates_path[pid])
print candidates.shape
for candidate in candidates:
y_batch = np.array(candidate, dtype='float32')
patch_center = candidate[:3]
batch = []
for i in range(self.tta):
batch.append(np.float32(self.data_prep_fun(data=img,
patch_center=patch_center,
pixel_spacing=pixel_spacing)))
x_batch = np.stack(batch)
print x_batch.shape
yield x_batch, y_batch, [pid]
def adjust_prediction(self, probability, image):
crf = dcrf.DenseCRF(np.prod(probability.shape), 2)
# crf = dcrf.DenseCRF(np.prod(probability.shape), 1)
binary_prob = np.stack((1 - probability, probability), axis=0)
unary = unary_from_softmax(binary_prob)
# unary = unary_from_softmax(np.expand_dims(probability, axis=0))
crf.setUnaryEnergy(unary)
# per dimension scale factors
sdims = [self.sdims] * 3
smooth = create_pairwise_gaussian(sdims=sdims, shape=probability.shape)
crf.addPairwiseEnergy(smooth, compat=2)
if self.schan:
# per channel scale factors
schan = [self.schan] * 6
appearance = create_pairwise_bilateral(sdims=sdims, schan=schan, img=image, chdim=3)
crf.addPairwiseEnergy(appearance, compat=2)
result = crf.inference(self.iter)
crf_prediction = np.argmax(result, axis=0).reshape(probability.shape).astype(np.float32)
return crf_prediction
def read_images( filenames, domain=None, image_size=64):
images = []
for fn in filenames:
image = cv2.imread(fn)
if image is None:
continue
if domain == 'A':
kernel = np.ones((3,3), np.uint8)
image = image[:, :256, :]
image = 255. - image
image = cv2.dilate( image, kernel, iterations=1 )
image = 255. - image
elif domain == 'B':
image = image[:, 256:, :]
image = cv2.resize(image, (image_size,image_size))
image = image.astype(np.float32) / 255.
image = image.transpose(2,0,1)
images.append( image )
images = np.stack( images )
return images
def plot_current_errors(self, epoch, counter_ratio, opt, errors):
if not hasattr(self, 'plot_data'):
self.plot_data = {'X':[],'Y':[], 'legend':list(errors.keys())}
self.plot_data['X'].append(epoch + counter_ratio)
self.plot_data['Y'].append([errors[k] for k in self.plot_data['legend']])
self.vis.line(
X=np.stack([np.array(self.plot_data['X'])]*len(self.plot_data['legend']),1),
Y=np.array(self.plot_data['Y']),
opts={
'title': self.name + ' loss over time',
'legend': self.plot_data['legend'],
'xlabel': 'epoch',
'ylabel': 'loss'},
win=self.display_id)
# errors: same format as |errors| of plotCurrentErrors
def mapFunction( x , y , func , ax = None, arrayInput = False, n = 10, title = None, **kwargs ) :
"""
Plot function on a regular grid
x : 1d array
y : 1d array
func : function to map
arrayInput : False if func(x,y) , True if func( [x,y] )
"""
if ax is None :
fig , ax = plt.subplots()
X , Y = np.meshgrid( x , y )
if not arrayInput :
Z = func( X.flatten() , Y.flatten() ).reshape(X.shape)
else :
Z = func( np.stack( [ X.flatten() , Y.flatten() ]) )
ax.contourf( X , Y , Z , n , **kwargs)
if title is not None : ax.set_title(title)
return ax
def process_observation(self, obs):
"""
Take in the current observation, do any necessary processing and return
the processed observation.
A return value of None indicates that there is no observation yet. A
random action will be taken.
"""
self.current_sequence.append(obs)
if len(self.current_sequence) < self.observations:
return None
if len(self.current_sequence) > self.observations:
self.current_sequence.pop(0)
# convert current sequence to input
# stacking essentially adds a single axis, want it to be after
obs_seq = np.stack(self.current_sequence, axis=len(obs.shape))
return obs_seq
def wavelength_to_XYZ(wavelength, observer='1931_2deg'):
''' Uses tristimulus color matching functions to map a awvelength to XYZ
coordinates.
Args:
wavelength (`float`): wavelength in nm.
observer (`str`): CIE observer name, must be 1931_2deg.
Returns:
`numpy.ndarray`: array with last dimension corresponding to X, Y, Z.
'''
wavelength = np.asarray(wavelength, dtype=config.precision)
cmf = get_cmf(observer)
wvl, X, Y, Z = cmf['wvl'], cmf['X'], cmf['Y'], cmf['Z']
ia = {'bounds_error': False, 'fill_value': 0, 'assume_sorted': True}
f_X, f_Y, f_Z = interp1d(wvl, X, **ia), interp1d(wvl, Y, **ia), interp1d(wvl, Z, **ia)
x, y, z = f_X(wavelength), f_Y(wavelength), f_Z(wavelength)
shape = wavelength.shape
return np.stack((x, y, z), axis=len(shape))
def XYZ_to_uvprime(XYZ):
''' Converts XYZ points to u'v' points.
Args:
XYZ (`numpy.ndarray`): ndarray with last dimension corresponding to
X, Y, Z.
Returns:
`tuple` containing:
`numpy.ndarray`: u' coordinates.
`numpy.ndarray`: u' coordinates.
'''
XYZ = np.asarray(XYZ)
X, Y, Z = XYZ[..., 0], XYZ[..., 1], XYZ[..., 2]
u = (4 * X) / (X + 15 * Y + 3 * Z)
v = (9 * Y) / (X + 15 * Y + 3 * Z)
shape = u.shape
return np.stack((u, v), axis=len(shape))
def Luv_to_chroma_hue(luv):
''' Converts L*u*v* coordiantes to a chroma and hue.
Args:
luv (`numpy.ndarray`): array with last dimension L*, u*, v*.
Returns:
`numpy.ndarray` with last dimension corresponding to C* and h.
'''
luv = np.asarray(luv)
u, v = luv[..., 1], luv[..., 2]
C = sqrt(u**2 + v**2)
h = atan2(v, u)
shape = luv.shape
return np.stack((C, h), axis=len(shape))
def uvprime_to_xy(uv):
''' Converts u' v' points to xyY x,y points.
Args:
uv (`numpy.ndarray`): ndarray with last dimension corresponding to
u', v'.
Returns:
`tuple` containing:
`numpy.ndarray`: x coordinates.
`numpy.ndarray`: y coordinates.
'''
uv = np.asarray(uv)
u, v = uv[..., 0], uv[..., 1]
x = (9 * u) / (6 * u - 16 * v + 12)
y = (4 * v) / (6 * u - 16 * v + 12)
shape = x.shape
return np.stack((x, y), axis=len(shape))
def __call__(self, process_func):
def wrapper(*args):
data_obj = args[1]
if (len(data_obj.shape) <= self.input_dim
or data_obj.shape[-1] == 1):
return process_func(*args)
else:
pool = mp.Pool(mp.cpu_count())# TODO: make configurable
arglist = [
(args[0],) +
(data_obj[...,i],) +
args[2:]
for i in range(data_obj.shape[-1])
]
result = pool.map(self.worker, arglist)
if self.output_dim > self.input_dim: # expanding
return np.stack(result, -1)
else: # contracting
return np.concatenate(result, -1)
return wrapper
def rvs(self, size=1):
'''
Generates random variates from the copula.
Parameters
----------
size : integer, optional
The number of samples to generate. (Default: 1)
Returns
-------
samples : array_like
n-by-2 matrix of samples where n is the number of samples.
'''
samples = np.stack((uniform.rvs(size=size), uniform.rvs(size=size)),
axis=1)
samples[:, 0] = self.ppcf(samples)
return samples
def plot_current_errors(self, epoch, counter_ratio, opt, errors):
if not hasattr(self, 'plot_data'):
self.plot_data = {'X':[],'Y':[], 'legend':list(errors.keys())}
self.plot_data['X'].append(epoch + counter_ratio)
self.plot_data['Y'].append([errors[k] for k in self.plot_data['legend']])
self.vis.line(
X=np.stack([np.array(self.plot_data['X'])]*len(self.plot_data['legend']),1),
Y=np.array(self.plot_data['Y']),
opts={
'title': self.name + ' loss over time',
'legend': self.plot_data['legend'],
'xlabel': 'epoch',
'ylabel': 'loss'},
win=self.display_id)
# errors: same format as |errors| of plotCurrentErrors
utils.py 文件源码
项目:kaggle-dstl-satellite-imagery-feature-detection
作者: u1234x1234
项目源码
文件源码
阅读 23
收藏 0
点赞 0
评论 0
def colorize_raster(masks):
''' (H, W, 10) -> (H, W, 3)
'''
assert masks.shape[2] == 10
palette = np.array([(180, 180, 180), (100, 100, 100), # Buildings, Misc.
(6, 88, 179), (125, 194, 223), # Road, Track
(55, 120, 27), (160, 219, 166), # Trees, Crops
(209, 173, 116), (180, 117, 69), # Waterway, Standing
(67, 109, 244), (39, 48, 215)], dtype=np.uint8) # Car
r = []
for obj_type in range(10):
c = palette[obj_type]
result = np.stack([masks[:, :, obj_type]] * 3, axis=2)
r.append(result * c)
r = np.stack(r)
r = np.max(r, axis=0)
return r
def load_embeddings(filename):
"""Loads embedings, returns weight matrix and dict from words to indices."""
weight_vectors = []
word_idx = {}
with codecs.open(filename, encoding='utf-8') as f:
for line in f:
word, vec = line.split(u' ', 1)
word_idx[word] = len(weight_vectors)
weight_vectors.append(np.array(vec.split(), dtype=np.float32))
# Annoying implementation detail; '(' and ')' are replaced by '-LRB-' and
# '-RRB-' respectively in the parse-trees.
word_idx[u'-LRB-'] = word_idx.pop(u'(')
word_idx[u'-RRB-'] = word_idx.pop(u')')
# Random embedding vector for unknown words.
weight_vectors.append(np.random.uniform(
-0.05, 0.05, weight_vectors[0].shape).astype(np.float32))
return np.stack(weight_vectors), word_idx