如何在张量流中对图像张量进行采样

时间:2016-10-08 13:26:29

标签: tensorflow tensorboard

我有一个形状为B*H*W*C的图像数据张量和一个形状为B*H*W*2的位置张量。位置张量中的值是像素坐标,我想根据这些像素坐标对图像数据张量中的像素进行采样。我尝试过一种方法,就像将张量重塑为一维张量一样,但我认为这真的很不方便。我想知道我是否可以通过一些更方便的方法来实现它,比如矩阵映射(例如opencv中的重映射)。

1 个答案:

答案 0 :(得分:0)

我首先会问你是否确定位置矩阵不是多余的。如果位置矩阵条目只对应于图像阵列中的像素位置,那么对于给定的应用程序,您可以使用位置矩阵代替图像数据。

或许作为起点,运行

package com.vidaflo.controllers;

import com.vidaflo.dto.RoomDto;
import com.vidaflo.model.location.Room;
import com.vidaflo.repositories.LocationRepository;
import com.vidaflo.services.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
public class RoomController {
    @Autowired
    private RoomService roomService;

    @Autowired
    private LocationRepository locationService;

    @PostMapping("/room/save")
    public String save(@RequestParam(name = "name") String name,
                       @RequestParam(name = "location_id") Long locationId) {
        roomService.save(name, locationService.findOne(locationId));
        return "room added";
    }

    @GetMapping("/room/all")
    public List<RoomDto> findAll() {
        return roomService.findAll().stream()
                .map(this::toDto)
                .collect(Collectors.toList());
    }

    private RoomDto toDto(Room room) {
        return RoomDto.builder()
                .id(room.getId())
                .name(room.getName())
                .build();
    }
}

会将张量转换为numpy数组,这可能会使您的操作更轻松 否则,1D张量并不是那么糟糕,并且有TF函数可以轻松地重塑。