- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我一直在努力尝试将一些 MNIST 数据库绘制到一个图像文件中,在我第一次尝试时,生成的图像似乎发生了偏移,如下所示:
我知道训练文件包含 60,000 张图像,每张图像的大小为 28 x 28 像素,在文件中表示为 28 x 28 x 60,000 uint8 的数组,其长度应为 47040000。
但是,当打印文件的长度时,它给出 47040016 作为它的长度,额外的 16 个数字是导致图像移动的原因。
使用的代码如下,const imgNum 由我要打印的图像和图像的长度定义。在读取图像文件时,我真的没有看到任何奇怪的事情。
package main
import (
"image"
"image/color"
"image/png"
"io/ioutil"
"os"
)
const (
imgSideLength = 28
imgSize = imgSideLength * imgSideLength
imgNum = 499 * imgSize
)
var images []uint8
func main() {
images, err := ioutil.ReadFile("train-images")
check(err)
canvas := image.NewRGBA(image.Rect(0, 0, imgSideLength, imgSideLength))
pixelIndex := imgNum
for i := 0; i < imgSideLength; i++ {
for j := 0; j < imgSideLength; j++ {
currPixel := images[pixelIndex]
pixelIndex++
pixelColor := color.RGBA{currPixel, currPixel, currPixel, 255}
canvas.Set(j, i, pixelColor)
}
}
numFile, err := os.Create("number.png")
check(err)
defer numFile.Close()
png.Encode(numFile, canvas)
}
func check(e error) {
if e != nil {
panic(e)
}
}
知道那 16 个像素是导致图像移动的像素,我决定修改 imgNum:
imgNum = 499 * imgSize + 16
有了这个改变,图像绘制得很好。
但我还是想知道为什么不应该有的地方多了16个数字?
最佳答案
查看their web site可以看到文件的数据格式是:
[offset] [type] [value] [description]
0000 32 bit integer 0x00000803(2051) magic number
0004 32 bit integer 60000 number of images
0008 32 bit integer 28 number of rows
0012 32 bit integer 28 number of columns
0016 unsigned byte ?? pixel
0017 unsigned byte ?? pixel
........
xxxx unsigned byte ?? pixel
这意味着头信息的前 16 个字节是 4 个 32 位整数,因此是 16 个字节。
关于file - ioutil ReadFile 添加额外的字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52247955/
我已经实现了一个 REST 服务,用于使用 IOUtils.copy() 下载一个中等大小 (250 Mb) 的 zip 文件,类似于 Most effective way to write file
我有一个Android 4.0/4.1用户上传的崩溃日志,但我无法重现。日志是这样的: java.io.IOException: close failed: EIO (I/O error) libco
复制方法给我这个错误: “IOUtils 类型中的方法 copy(InputStream, OutputStream) 不适用于参数 (FileInputStream, StringWriter, S
我正在尝试将我的Stanford CoreNLP版本升级到最新版本(之前是v3.5.2,尝试升级到v3.6.0)。编译完 v3.6.0 所需的所有新 jar 后,我启动了一个独立的斯坦福 CoreNL
我使用以下代码在我的 android 应用中创建了一个临时文件: public File streamToFile (InputStream in) throws IOException {
用途:IO工具类(将内容写到流中) 使用场景 IO工具类只是辅助流的读写,并不负责关闭流。原因是流可能被多次读写,读写关闭后容易造成问题。 (将内容写到流中) (将内容写到流中) (将内容
我正在使用 Apache IOUtils 下载文件。为了恢复下载,我使用以下代码: IOUtils.copyLarge(input,output,localFileSize,fileLengthOnS
我需要将 pdf 文件编码/解码为 Base64 格式。所以我从磁盘读取文件到String(因为我将来会收到String Base64格式的文件); String pdfString = IOUtil
我是Golang的新手。 我试图了解如何使用ioutil.ReadAll(非HTTP响应)。根据源代码(和文档): // ReadAll reads from r until an error or
这个问题在这里已经有了答案: 404 page not found - Go rendering css file (1 个回答) No such file or directory error in
我有一个 servlet,它使用具有以下逻辑的方法: ServletOutputStream out = response.getOutputStream(); IOU
我在下面的代码行中收到 OOM 错误。有办法解决吗?我尝试增加堆大小但没有用。 FileInputStream inputDoc = new FileInputStream(inputDoc1); b
使用 IOUtils.write 将字符串写入文件 try { IOUtils.write("test", new FileWriter(configFile)); }
我有一个类,它打开 HTTP 服务器并监听端口。回复是一个 http-header 加上一个 json 对象。该类从服务器获取输入流,将其转换为字符串,过滤 http header 并解析 json
我的应用程序中有两个文件到字符串的进程(一个实际上处理 Assets 文件)。 如果我对同一个文件重复这些过程中的任何一个几次,我会得到 OutOfMemoryErrors。 我怀疑这可能是因为我没有
我不是 Java 开发人员,但只需要了解一些 Java 正在做什么。 我有一些代码 IOUtils.copy(InputStream a, Writer b, "ISO-8859-1") 或类似的文字
我注意到似乎没有办法指定复制方法的字节长度。 copyLarge(InputStream input, OutputStream output, long inputOffset, long leng
这是我的情况:我正在使用 IOUtils 复制文件。接下来我要做的是向另一个程序发送一条 JSON 消息,表示“您可以下载副本”。问题是大约 25% 的时间其他程序收到错误消息“收到意外的 EOF 下
我在 android 上编写应用程序,它将 xml 文件发送到 PHP 服务器。这是我的代码: InputStream is = new FileInputStream(file); HttpClie
我尝试压缩超过 100Mb 的大型视频文件。 public static void compress(File input, File output) throws IOException {
我是一名优秀的程序员,十分优秀!