I am trying to create a LMDB with 6 concatenated images as labels. My python script looks like this:
in_db = lmdb.open('image-lmdb', map_size=int(1e12), writemap=True)
with in_db.begin(write=True) as in_txn:
for in_idx, in_ in enumerate(inputs):
im = np.array(Image.open('train_label_1/' + in_))
im = im[:,:,1]
c.append(im)
im = np.array(Image.open('train_label_2/' + in_))
im = im[:,:,1]
c.append(im)
im = np.array(Image.open('train_label_3/' + in_))
im = im[:,:,1]
c.append(im)
im = np.array(Image.open('train_label_4/' + in_))
im = im[:,:,1]
c.append(im)
im = np.array(Image.open('train_label_5/' + in_))
im = im[:,:,1]
c.append(im)
im = np.array(Image.open('train_label_6/' + in_))
im = im[:,:,1]
c.append(im)
d = np.array(c)
im_dat = caffe.io.array_to_datum(d)
in_txn.put('{:0>10d}'.format(in_idx), im_dat.SerializeToString())
in_db.close()
I have two problems:
How big should the map_size be?
I have round about 140,000 labels. Each image inside the label has a size of 45 x 45. Because I am using only one dimension of each image I guess that every pixel is one byte. So my guess would be that the map_size should be 45*45*6*140,000 Bytes. But if I set the map_size to this value, I get a MapFullError: mdb_put: MDB_MAP_FULL: Environment mapsize limit reached after 528 labels.
If I set the map_size to 1e12 I don't get an MapFullError that fast. But instead the RAM usage goes to nearly 100%. Is there a way to limit RAM usage when creating a lmdb?
EDIT
To reduce the RAM usage I tried to iterate over the lmdb as described here:
for idx in range(int(math.ceil(len(inputs)/1000.0))):
in_db = lmdb.open('image-lmdb', map_size=int(1e12))#, writemap=True
with in_db.begin(write=True) as in_txn:
for in_idx, in_ in enumerate(inputs[(1000*idx):(1000*(idx+1))]):
im = np.array(Image.open('train_label_1/' + in_))
im = im[:,:,1]
c.append(im)
im = np.array(Image.open('train_label_2/' + in_))
im = im[:,:,1]
c.append(im)
.
.
.
d = np.array(c)
im_dat = caffe.io.array_to_datum(d)
in_txn.put('{:0>10d}'.format(in_idx + idx * 1000), im_dat.SerializeToString())
in_db.close()
But still the RAM usage gets to 99% and the writing of the lmdb slows very much down.
I found my mistake. I forgot to reset the array c after each iteration.
Related
I am trying to create 2D heatmap from those two parameters:
diffQ = discharge(2:lgth) - discharge(1:lgth-1);
difft = time(2:lgth) - time(1:lgth-1);
My former colleague made code for just one parameter:
diff = discharge(2:lgth) - discharge(1:lgth-1);
bin = 1;
idiff = int32(diff);
mx = max(idiff);
mn = min(idiff);
msh = ones(((mx-mn)/bin)+2);
for i = 1:lgth-2
y = ((idiff(i)-mn)/bin)+1;
x = (((idiff(i+1)-mn)/bin)+1);
msh(x,y) = msh(x,y) + 1;
end
mshl = log(log(log(msh)+1)+1);
newplot();
surface(mshl,'EdgeColor','none');
colormap(heatMapA);
but I can't figure out how to change plot creating for my case.
Thanks a lot
Kozina
I have plotted two graphs using plotly dash. But when the y-axis / x-axis tick size is more it gets cut off.
Y-axis :
Code :
data = [go.Scatter(x = df[df['S2PName-Category']==category]['S2BillDate'],
y = df[df['S2PName-Category']==category]['totSale'],
mode = 'markers+lines',
name = category) for category in df['S2PName-Category'].unique()]
layout = go.Layout(title='Category Trend',
xaxis = dict(title = 'Time Frame', tickformat = '%d-%b-%y'),
yaxis = dict(tickprefix= '₹', tickformat=',.2f',type='log'),
hovermode = 'closest',
plot_bgcolor = colors['background'],
paper_bgcolor = colors['background'],
font = dict(color = colors['text'])
)
X-Axis :
Code :
data = [go.Scatter(x = df[df['S2PName']==item]['S2BillDate'],
y = df[df['S2PName']==item]['totSale'],
mode = 'markers+lines',
name = item) for item in items]
layout = go.Layout(title='Category Trend',
xaxis = dict(title = 'Time Frame' , tickformat = '%d-%b'),
yaxis = dict(tickprefix= '₹', tickformat=',.2f',type='log',autorange = True),
hovermode = 'closest',
plot_bgcolor = colors['background'],
paper_bgcolor = colors['background'],
font = dict(color = colors['text'])
)
In the above 2 graphs , as the length of the tick value increases, it gets cut off . Is there a better way to handle this ?
Credit for #Flavia Giammarino in comments for the reference to the docs. I'm posting the answer for completeness.
https://plotly.com/python/setting-graph-size/
From that link the example below shows how to set margin:
fig.update_layout(
margin=dict(l=20, r=20, t=20, b=20),
)
Where l r t b correspond to left, right, top, bottom.
I had a similar problem with some Dash/Plotly charts and long y axis labels being truncated or hidden. There didn't seem to be much information or documentation on this issue, so it took a while to solve.
Solution: add this code to the layout settings to prevent truncation of the y axes labels:
fig.update_layout(
yaxis=dict(
automargin=True
)
)
or you can update the yaxes setting specifically:
fig.update_yaxes(automargin=True)
Update: I tried another version of Plotly (5.10 or above) which mentions setting the automargin setting to any combination of automargin=['left+top+right+bottom'] with similar results. This still seems a bit unstable and doesn't solve all possible scenarios or corner cases, but works fine in most cases, especially when the browser window is maximized.
I am following Jeff Heaton's tutorial how to create a GAN with keras. Everything works fine even with my own dataset. However, I cannot figure out how to create a single new images. (Mr. Heaton creates 28 images in form of a collages!)
What I tried without success:
new_fixed_seed = np.random.normal(0, 1, (1, 100))
generated_images = generator.predict(new_fixed_seed)
im = Image.fromarray(generated_images)
Result: TypeError: Cannot handle this data type
What am I doing wrong?
Normally, for me when I calculate generated images I am using the following code for storing them locally:
# combine a squared number of images.
def combine_images(generated_images):
generated_images = np.transpose(generated_images , (0, 3, 1, 2))
num = generated_images.shape[0]
width = int(math.sqrt(num))
height = int(math.ceil(float(num)/width))
shape = generated_images.shape[2:]
image = np.zeros((3,(height+3)*shape[0], (width+3)*shape[1]),
dtype=generated_images.dtype)
for index, img in enumerate(generated_images):
new_shape = (img.shape[0], img.shape[1]+ 4, img.shape[2] + 4)
img_ = np.zeros(new_shape)
img_[:, 2: 2+img.shape[1], 2: 2+img.shape[2]] = img
i = int(index/width)
j = index % width
image[:, i*new_shape[1]: (i+1)*new_shape[1], j*new_shape[2]: (j+1)*new_shape[2]] = img_[:, :, :]
return image
# store combined images
def store_image_maps(images_db, filename):
image = combine_images(images_db)
image = image * 127.5 + 127.5
image = np.swapaxes(image, 0, 1)
image = np.swapaxes(image, 1, 2)
cv2.imwrite(filename,image)
I got it to work but I am not completely satisfied with it as I think it could be cleaner and I think there are unnecessary steps involved:
# SEED_SIZE is 100
fixed_seed = np.random.normal(0, 1, (1, SEED_SIZE))
# used 64x64 because those were my image sizes
image_array = np.full((
64,64, 3),
255, dtype=np.uint8)
generated_images = generator.predict(fixed_seed)
#if you don't use 255 here the images are black
image_array[:] = generated_images * 255
im = Image.fromarray(image_array)
im.save('/content/drive/My Drive/Dataset/test.png')
I have the following mixed effects model:
p1 <- lmer(log(price) ~ year*loca + (1|author), data = df)
'year' is continuous
'loca' is categorical variable with 2 levels
I am trying to plot the significant interaction from this model.
The following code (using the visreg package) plots the lines from each of the two 'loca' but it does not produce a 95% confidence band:
visreg(p1, "year", by = "loca", overlay = T,
line=list(lty = 1, col = c("grey", "black")), points=list(cex=1, pch=19,
col = c("grey", "black")), type="conditional", axes = T)
Then, I tried the following code which allows me to plot the lines, but with no data points on top and no CIs:
visreg(p1, "year", by = "loca", overlay = T,
line=list(lty = 1, col = c("grey60", "black")), points=list(cex=1,
pch=19, col = c("grey", "black")),
type="conditional", trans = exp, fill.par = list(col = c("grey80",
"grey70")))
I get CI bands when I use type = 'contrast' rather than 'conditional'. However, this doesn't work when I try to backtransform the price as above using trans = exp.
Overall I need to be able to plot the interaction with the following attributes:
Confidence bands
backtransformed points
predicted line (one for each level of 'loca')
More than happy to try other methods....but I can't seem to find any that work so far.
Help much appreciated!
one possibility is with the use of the effects package:
library(effects)
eff.p1 <- effect("year*loca", p1, KR=T)
then you could either directly plot it with what the package provides and customize it from there:
plot(eff.p1)
or take what effect produces and plot it with ggplot in a nicer plot:
eff.p1 <- as.data.frame(eff.p1)
ggplot(eff.p1, aes(year, linetype=factor(loca),
color = factor(loca))) +
geom_line(aes(y = fit, group=factor(loca)), size=1.2) +
geom_line(aes(y = lower,
group=factor(loca)), linetype =3) +
geom_line(aes(y = upper,
group=factor(loca)), linetype =3) +
xlab("year") +
ylab("Marginal Effects on Log Price") +
scale_colour_discrete("") +
scale_linetype_discrete("") +
labs(color='loca') + theme_minimal()
I can't really try the code without the data, but I think it should work.
This should do the trick:
install.packages(sjPlot)
library(sjPlot)
plot_model(p1, type = "int", terms = c(year,loca), ci.lvl = 0.95)
Although it comes out with some warnings about labels, testing on my data, it does the back transformation automatically and seems to work fine. Customising should be easy, because I believe sjPlot uses ggplot.
EDIT: #Daniel points out that alternative options which allow more customization would be plot_model(type = "pred", ...) or plot_model(type = "eff", ...)
I have a table with about 15 content controls. The content controls have different titles.
Now, I copy-paste the table with content controls a couple of times, and later, get different values into every single content control from the database. Since the content controls from different tables share the same name, I thought of looping through number of tables using something like this
seqNo = 1
For Each t in MyTables
ActiveDocument.SelectContentControlsByTitle("title1").Item(seqNo).Range.Text = "some value 1 from DB"
ActiveDocument.SelectContentControlsByTitle("title2").Item(seqNo).Range.Text = "some value 2 from DB"
' and so on
seqNo = seqNo + 1
Next
The problem is when I use this code, my content controls don't get filled in sequentially. I mean, for example, content control with title title1 from table1 isn't filled with its value, instead, content control with title title1 from table4 gets that value. And this mess goes around really bad: values from table 2 can end up in table 4, 9, 10 and so forth.
I think the order of content controls gets messed up somehow when I copy-paste the tables.
And clue how to get it right?
Didn't really find why this happens, but went with giving unique names to the content controls, like title1, title2, and so on, and then looping through all of them to set the needed values.
Oh my god yes... I have stumbled upon the same annoying issue too. My workaround has been after the copy change the title in code then paste and change that one too (see below). Now my issue is that this takes WAY too long to run since I'm filling out many of these templates in my code. I'm currently at a lose as how to speed this process up or a different approach I should been using.
objWord.ActiveDocument.Range(start:=objWord.ActiveDocument.Tables(3).Range.Rows(1).Range.start, End:=objWord.ActiveDocument.Tables(3).Range.Rows(5).Range.End).Copy
objWord.Selection.EndKey Unit:=wdStory
objDoc.SelectContentControlsByTitle("Date").Item(1).Title = "Date1"
objDoc.SelectContentControlsByTitle("StartTime").Item(1).Title = "StartTime1"
objDoc.SelectContentControlsByTitle("EndTime").Item(1).Title = "EndTime1"
objDoc.SelectContentControlsByTitle("Mins").Item(1).Title = "Mins1"
objDoc.SelectContentControlsByTitle("Note").Item(1).Title = "Note1"
objDoc.SelectContentControlsByTitle("Grp").Item(1).Title = "Grp1"
objDoc.SelectContentControlsByTitle("acc1").Item(1).Title = "acc1_1"
objDoc.SelectContentControlsByTitle("acc2").Item(1).Title = "acc2_1"
objDoc.SelectContentControlsByTitle("acc3").Item(1).Title = "acc3_1"
objDoc.SelectContentControlsByTitle("acc4").Item(1).Title = "acc4_1"
objDoc.SelectContentControlsByTitle("acc5").Item(1).Title = "acc5_1"
objDoc.SelectContentControlsByTitle("acc6").Item(1).Title = "acc6_1"
objDoc.SelectContentControlsByTitle("acc7").Item(1).Title = "acc7_1"
objDoc.SelectContentControlsByTitle("acc8").Item(1).Title = "acc8_1"
For j = 2 To UBound(Narray)
objWord.Selection.Paste
objDoc.SelectContentControlsByTitle("Date").Item(1).Title = "Date" & j
objDoc.SelectContentControlsByTitle("StartTime").Item(1).Title = "StartTime" & j
objDoc.SelectContentControlsByTitle("EndTime").Item(1).Title = "EndTime" & j
objDoc.SelectContentControlsByTitle("Mins").Item(1).Title = "Mins" & j
objDoc.SelectContentControlsByTitle("Note").Item(1).Title = "Note" & j
objDoc.SelectContentControlsByTitle("Grp").Item(1).Title = "Grp" & j
objDoc.SelectContentControlsByTitle("acc1").Item(1).Title = "acc1_" & j
objDoc.SelectContentControlsByTitle("acc2").Item(1).Title = "acc2_" & j
objDoc.SelectContentControlsByTitle("acc3").Item(1).Title = "acc3_" & j
objDoc.SelectContentControlsByTitle("acc4").Item(1).Title = "acc4_" & j
objDoc.SelectContentControlsByTitle("acc5").Item(1).Title = "acc5_" & j
objDoc.SelectContentControlsByTitle("acc6").Item(1).Title = "acc6_" & j
objDoc.SelectContentControlsByTitle("acc7").Item(1).Title = "acc7_" & j
objDoc.SelectContentControlsByTitle("acc8").Item(1).Title = "acc8_" & j
Next