How to make WGAN train with RGB images instead of grayscale? - generative-adversarial-network

I'm trying to implement WGAN-GP from this code
https://github.com/eriklindernoren/Keras-GAN/blob/master/wgan_gp/wgan_gp.py
Instead of the default mnist dataset, I'd like to use my own dataset of images that are larger (384x384) and RGB rather than black and white.
I tried changing the number of channels from 1 to 3 to take the colour channels into accoutn but I'm still running into the error below when training.
Still new to this and I'm unsure of any other modifications I'd need to make my code run.
When I use train()
ValueError: Error when checking input: expected input_28 to have 4 dimensions, but got array with shape (32, 384, 384, 1, 3)

Related

Convert RGB colors to DMC floss colors

I'm trying to find an automated way to calculate what DMC color is closest to a given RGB. I found this site:
http://www.damaniel.info/dmc/dmctorgb.html
And that does what I want. However, I have to type each RGB is separately and I have a lot of them.
I'd like to use formulas to automate the calculate. Preferably in Excel or in R. I haven't had luck with Selenium (and don't have much spare time right now to get up to speed), so that might not be the best option for doing automated form filling with the above site.
Any ideas?
The DMC R package (1) will help with that. It looks like it only supports hexadecimal codes, but you can convert RGB colors using the rgb function from the grDevices package (Use maxColorValue = 255 if you are using RGB versus rgb).
# install.packages("devtools")
# devtools::install_github("sharlagelfand/dmc")
library(dmc)
# convert rbg to hex
# second green color from ColorBrewer.org
# (3-class BuGn second color)
color <- grDevices::rgb(red = 153, green = 216, blue = 201, maxColorValue = 255)
# find dmc - return a few more results
color_dmc <- dmc(color, n = 3)
color_dmc
If you have a few colors, you can wrap this in a loop or apply function and write the dmc values to a master object by extracting the elements you need (i.e., color_dmc$hex, color_dmc$name, etc.)
1 https://github.com/sharlagelfand/dmc
There's a list of RGB values corresponding to DMC floss colors here: http://my.crazyartzone.com/dmc.asp . There's also a list built in to the source code for the web page you found (and it's different). You can see that list at this URL: http://www.damaniel.info/dmc/dmctorgb.js .
Why are they different? There are different standards for RGB. The most common one is now called sRGB (for "Standard RGB"). They might be using different standards. Or maybe they are getting their colors from different sources. They are approximate, anyway: floss varies in color. So just pick one.
Then what you would need to do is to take your RGB value and find the nearest value in one of those lists.
This isn't trivial, because defining "nearest" is hard. What that web page appears to do is to use Euclidean distance in RGB space, but that space is far from perceptually uniform. So it's probably best to convert both your input RGB color and the table of floss colors to some other representation where equal distances are perceptually equal. I think the LAB space (see ?colorspace::LAB) is probably a good choice for that, but I don't know if it's best. Read http://colorspace.r-forge.r-project.org/articles/color_spaces.html if you want to dig into the details.
So here's what you need to do:
Load a version of one of those tables into a dataframe.
Convert all the RGB values there into LAB values, and add those to the dataframe.
Write a function to convert your input RGB value to LAB.
Find the nearest LAB entry in the dataframe to the LAB value you want, and output the corresponding floss number.
EDITED TO ADD: While I was writing that, #dcruvolo pointed out the nice dmc package. So use that! (Maybe you want to change how it does distance; you really don't want the default Euclidean distance in RGB space.)

How can I generate heatmaps from specific sections of data using GnuPlot? ('splot', 'every', 'using' incompatibilites etc.)

I am attempting to generate heatmaps from a data file I've been generating. I could re-format the data however I like, but for the time being, let's say it's a list of 16 numbers that I'd like put into a 4x4 heatmap. However, I have many sets of these 16 numbers sequentially in the same file, and hope to eventually animate them together (something I am more comfortable with, and will come later)
However, for the time being, I cannot find a way to get GnuPlot to select only certain sections of the data file while still plotting properly. A loose example of what I would've thought it WOULD look like:
plot "SortedData.txt" every ::0::15 w image
or:
splot "SortedData.txt" every ::0::15
Both give me errors and fail to render. I could label the data values with an x-y coordinate if needed, but the task is fairly repetitive: I just want the first 16 points mapped, and then the ability to iterate once and have the next 16 points mapped on their own, etc. Stripping the data file to just the first 16 points and removing the 'every' command confirms that it can plot, but trying to specify even just the first 16 manually messes it up.
Can anyone point me in the right direction? The "every" command has been fairly nebulous and seems largely incompatible with images / 3-D data. Also, I am running on Windows, so piping in linux commands is something I'd like to avoid.
Thanks!
edit: Here is 4 example frames of the data. Reformatting it to, say, present as a matrix or label with pixel addresses are all something I can do if needed.
0.000000 -49.314654 -44.425234 -46.613870 -48.494232 -46.884806 -46.553071 -46.555624 -43.755972 -47.817691 -42.481637 -46.819782 -44.347586 -49.487077 -47.291832 -45.140636 -47.945934
0.839906 -49.325396 -44.425493 -46.613214 -48.501283 -46.887236 -46.550858 -46.555285 -43.752786 -47.814706 -42.453793 -46.814333 -44.329492 -49.493501 -47.289394 -45.133555 -47.944045
1.679721 -49.336151 -44.425787 -46.612573 -48.508348 -46.889684 -46.548645 -46.554958 -43.749626 -47.811707 -42.425757 -46.808866 -44.311344 -49.499930 -47.286951 -45.126476 -47.942155
2.519466 -49.346920 -44.426117 -46.611946 -48.515427 -46.892152 -46.546431 -46.554641 -43.746492 -47.808695 -42.397525 -46.803382 -44.293140 -49.506365 -47.284501 -45.119398 -47.940264
It seems that each line in your data file has 17 elements. I assume that the first column is not part of your image data. I would format the remaining 16 values as a 4x4 matrix, with each frame separated by two blank lines:
-49.314654 -44.425234 -46.613870 -48.494232
-46.884806 -46.553071 -46.555624 -43.755972
-47.817691 -42.481637 -46.819782 -44.347586
-49.487077 -47.291832 -45.140636 -47.945934
-49.325396 -44.425493 -46.613214 -48.501283
-46.887236 -46.550858 -46.555285 -43.752786
-47.814706 -42.453793 -46.814333 -44.329492
-49.493501 -47.289394 -45.133555 -47.944045
-49.336151 -44.425787 -46.612573 -48.508348
-46.889684 -46.548645 -46.554958 -43.749626
-47.811707 -42.425757 -46.808866 -44.311344
-49.499930 -47.286951 -45.126476 -47.942155
-49.346920 -44.426117 -46.611946 -48.515427
-46.892152 -46.546431 -46.554641 -43.746492
-47.808695 -42.397525 -46.803382 -44.293140
-49.506365 -47.284501 -45.119398 -47.940264
You can then visualize each frame with the command
plot "data.dat" index FRAME matrix w image
where FRAME is 0, 1, 2 or 3.

increase png resolution when using saveHTML() {animation} in R

I am in need of a way to increase the resolution of the png files created by saveHTML().
Here is a dummy script of what I am trying to do where in reality plot() is a number of nested loops:
x<-y<-rep(1,10)
saveHTML( for (i in 1:10){
plot.new()
plot.window(xlim=c(0,10),ylim=c(0,10))
plot(x[i],y[i])
}
,ani.dev="png",img.name="test",htmlfile="test")
A few things I have tried:
1) increase the animation size using ani.options(ani.height,ani.width) but I only get a larger grainy image.
2) call png() device inside the saveHTML expression and set the resolution there, but ultimately I dont get any figures.
3) call a new windows() device for plotting and setting the window size, but again this does not increase the resolution.
The most straight forward work-around that I came across is to create hi-res pngs and animate using ffmpeg. But I am not ready to re-work my script just yet.
Has anyone found a way to increase png resolution inside the saveHTML() function?
Instead of passing ani.dev="png", you can pass ani.dev = function(...){png(res=75*grain,...)}, where grain is some number > 1. If you specify the options ani.height and/or ani.width and multiply these values by the same factor grain, then you effectively increase the pixel resolution of the output by this factor.
N.B.: the default resolution 75 above might be machine dependent, I did not see it documented.

How to code edge attributes as vertex attributes using igraph in R

I am graphing a network and trying to color the vertices using non-overlapping attributes. I want my network diagram to be colored according to different attributes. In this example, if the first three letters of ID 2 are equal to U 50 or U 51, I want this to show up as red. I have 5 attributes I want this graph coded by and any observations that don't fall into one of the categories should be coded in a default color. In this way I will be able to see the intensity of these attributes and better communicate this to other people. So far, I have been unable to get the code to work using a variety of different coding methods. First I tried to create a new variable that assigned the correct attribute to each observation before converting it into an i graph object.
anon.nd$vertexcolor[substr(anon.nd$ID2,1,3)=="U50" | substr(anon.nd$ID2,1,3)=="U51"]<-"O"
anon.nd$vertexcolor[substr(anon.nd$ID2,1,3)=="U54" | substr(anon.nd$ID2,1,3)=="U55"]<-"P"
anon.nd$vertexcolor[anon.nd$INT.type=="K1"]<-"INT.NB"
anon.nd$vertexcolor[anon.nd$Country=="L12"]<-"UK"
anon.nd$vertexcolor[anon.nd$ID2=="U769"]<-"OBL"`
I then specified the colors I wanted to assign to each each attribute. I used the get vertex attribute code and filled in the appropriate colors.
anon.nd1<-graph.data.frame(anon.nd)
vertex_colors=get.vertex.attribute(anon.nd1,"vertexcolor")
colors=c('azure3', 'firebrick1', 'orange1', 'darkblue', 'darkolivegreen', 'gold')
vertex_colors[vertex_colors==0]=colors[1]
vertex_colors[vertex_colors==1]=colors[2]
vertex_colors[vertex_colors==2]=colors[3]
vertex_colors[vertex_colors==3]=colors[4]
vertex_colors[vertex_colors==4]=colors[5]
vertex_colors[vertex_colors==5]=colors[6]
I tried this same method using just:
vertex_colors<-vertex_colors+1
Then to plot, I changed my edge color to black, specified my layout, and change the size of my edges and vertices.
E(anon.nd1)$color="black"
nd.layout<-layout.fruchterman.reingold(anon.nd1)
plot(anon.nd1, layout=nd.layout, vertex.color=vertex_colors, vertex.size=2, edge.arrow.size=.01, vertex.label=NA)
Using this method, no color shows up on the vertices, not even the default color. Using a different method where I set the vertex attribute, I do a little better. The default color shows up, but the colors I want do not.
anon.nd2<-graph.data.frame(anon.nd)
V(anon.nd2)$colors<-"azure3"
V(anon.nd2)$colors[substr(anon.nd2$ID2,1,3)=="U50" | substr(anon.nd2$ID2,1,3)=="U51"]<-"firebrick1"
V(anon.nd2)$colors[substr(anon.nd2$ID2,1,3)=="U54" | substr(anon.nd2$ID2,1,3)=="U55"]<-"orange1"
V(anon.nd2)$colors[anon.nd2$Country=="L12"]<-"darkblue"
V(anon.nd2)$colors[anon.nd2$INT.type=="K1"]<-"darkolivegreen"
V(anon.nd2)$colors[anon.nd2$ID2=="U769"]<-"gold"
E(anon.nd2)$color<-"black"
nd.layout<-layout.fruchterman.reingold(anon.nd2)
windows(width=20, height=16)
plot(anon.nd2, layout=nd.layout, vertex.size=2, edge.arrow.size=.01, vertex.label=NA, vertex.color="vertex_colors")
I think the problem might be that I am trying to code vertex color using multiple (non-overlapping) edge attributes. But I don't know how to convert and edge attribute into a vertex attribute. I also don't know if there is some other, unidentified problem with my code.
Here is the link to my data is copied below as well as a link to my full code file which has one or two other methods I tried using to solve this problem. Any help would be much appreciated!
Data
And here is an R file with my code, which is also above: R-file
I think you are messing up your vertex_color vector, have a look at it with head().
anon.nd$vertexcolor[anon.nd$INT.type=="K1"]<-"INT.NB"
vertex_colors[vertex_colors==0]=colors[1]
You first assign a string and then compare with numbers, so non of them should be true.
plot(anon.nd2, layout=nd.layout, vertex.size=2, edge.arrow.size=.01, vertex.label=NA, vertex.color="vertex_colors")
This contains a typo and returns an error for me since "vertex_colors" isn't a colour name.
Last but not least, does
plot(anon.nd2, vertex.color=colors)
or
plot(anon.nd2, vertex.color=1:8)
result in a colourful plots? If yes, the vertex_colors vector is your problem, if not something else is.

Using transparency settings on a kernel plot, R

Hi I am trying to plot and overlay multiple kernel density estimates into a single plot (using KS library). Since I want to overlay multiple kernels I am "playing" with the transparency color settings so that the final plot is easier to understand. However, for some reason when I plot it, it shows a grid pattern with the color color/transparency filling which I don't know how to remove so that the color is all uniform...
These are the options that I use to export my plot as a PDF,
pdf(paste(database$species[1],".pdf",sep=""),width=11,height=8,paper="a4r",pointsize=15)
par(mfrow=c(1,1))
par(mar=c(5,4,4,3))
This is the code that I use to make the plot and overlay it with others,
# plot of KUD-Depth utilization
plot(ddhat.day,cont=c(95),lwd=1,add=F,display="filled.contour2",col=c(NA,"#8B000040"),xaxs="i",yaxs="i",
plot(ddhat.night,cont=c(95),lwd=1.5,add=TRUE,display="filled.contour2",col=c(NA,"#00008020"),
xlab="",ylab="",las=1,ann=F,bty="l",cex=0.6,yaxs="i",xaxs="i")
xlab="",ylab="",las=1,ann=F,bty="l",cex=0.6,xlim=c(0,max(dd[,1]+dd[,1]*0.6)),ylim=c(50,-10))
plot(ddhat.day,cont=c(50),add=T,display="filled.contour2",col=c(NA,"#FF000040"),lty=2,lwd=1,
xlab="",ylab="",cex=0.6)
plot(ddhat.night,cont=c(50),add=TRUE,display="filled.contour2",col=c(NA,"#00BFFF40"),lwd=1.5,
xlab="",ylab="",cex=0.6)
plot(ddhat.day,cont=c(0,0),drawpoints=TRUE,col="black",ptcol="grey15",cex=0.45,add=TRUE)
plot(ddhat.night,cont=c(0,0),drawpoints=TRUE,col="black",ptcol="grey15",cex=0.45,add=TRUE,pch=4)
dev.off()
If anybody has an idea why when I use this transparency option for color it shows a grid too and ho to remove it so it shows a smooth surface that would be fantastic!
You have a really weird way of specifying col. Instead of those NAs which I'm pretty sure aren't supposed to be there, try using the rgb function. It takes (at least) 4 arguments, which are the red, green, blue and alpha (transparency) channels, expressed as fractions.
col=rgb(0, 0, 0, 1) # black
col=rgb(0, 1, 1, .5) # green + blue = cyan, 50% opaque
col=rgb(1, 0, 0, .2) # red, 20% opaque = 80% transparent
This is a pretty old question but I thought I'd throw my response out there for future reference. The grid that you are seeing (if it's the grid I think you are referring to) is the result of creating a raster graphic in a pdf file. The pdf does a poor job of rendering the raster. If you want to make the grid (really thin white lines, right?) go away, try saving the file as a native raster file type (eg., jpeg(),tiff).
In most cases, outputting an R graphic as a pdf() works great because then you have a nice, scalable vector graphic for which you don't have to worry about resolution. However, when you create figures like an image plot sometimes it gets wonky (this only happens on my Mac though and not on my PC).
Some code below may illustrate. If you run the pdf version (on a mac) you will get the figure below. If you run the jpg version it will go away.
library(MASS)
library(ks)
data(iris)
fhat <- kde(x=iris[,1:2])
jpeg("test.jpg")
plot(fhat, display="filled.contour2", cont=seq(10,90,by=10))
dev.off()
pdf("test.pdf")
plot(fhat, display="filled.contour2", cont=seq(10,90,by=10))
dev.off()

Resources