模型的自转化

package 反射的学习;

import java.util.Map;

public interface Yinshe {

	public Map<String, String> getYinsheGx();
}

package 反射的学习;


public class User {
	 private int age;
	 private String name;
	 private String name1;
	    public User() {
	        super();
	    }
	    public User(String name) {
	        super();
	        this.name = name;
	    }
	    public User(int age, String name) {
	        super();
	        this.age = age;
	        this.name = name;
	    }
	    public User(int age, String name,String name1) {
	        super();
	        this.age = age;
	        this.name = name;
	        this.name1 = name1;
	    }
	    public int getAge() {
	        return age;
	    }
	    public void setAge(int age) {
	        this.age = age;
	    }
	    public String getName() {
	        return name;
	    }
	    public void setName(String name) {
	        this.name = name;
	    }
	    @Override
	    public String toString() {
	        return "User [age=" + age + ", name=" + name + "]";
	    }
		public String getName1() {
			return name1;
		}
		public void setName1(String name1) {
			this.name1 = name1;
		}
}
package 反射的学习;


public class User1 {
	 private int age;
	 private String name;
	 private String name1;
	 public User1() {
	        super();
	    }
	    public User1(String name) {
	        super();
	        this.name = name;
	    }
	    public User1(int age, String name) {
	        super();
	        this.age = age;
	        this.name = name;
	    }
	    public User1(int age, String name,String name1) {
	        super();
	        this.age = age;
	        this.name = name;
	        this.setName1(name1);
	    }
	    public int getAge() {
	        return age;
	    }
	    public void setAge(int age) {
	        this.age = age;
	    }
	    public String getName() {
	        return name;
	    }
	    public void setName(String name) {
	        this.name = name;
	    }
	    @Override
	    public String toString() {
	        return "User1 [age=" + age + ", name=" + name + "]";
	    }
		public String getName1() {
			return name1;
		}
		public void setName1(String name1) {
			this.name1 = name1;
		}
		
}
package 反射的学习;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class 反射获取属性 {

	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, NoSuchFieldException {
		List<User> lt=new ArrayList<User>();
		lt.add(new User(1212,"11"));
		lt.add(new User(1212,"11"));
		
		System.out.println(zhuanhuaObject(new User(1212,"11","11")));
	
		System.out.println(zhuanhuaList(lt).size());

	}
	/**
	 * 对象的自转换
	 * @param user
	 * @param user1
	 * @return
	 * @throws ClassNotFoundException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 * @throws InvocationTargetException
	 * @throws NoSuchMethodException
	 * @throws SecurityException
	 * @throws NoSuchFieldException
	 */
	public static User1 zhuanhuaObject(Object obj) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, NoSuchFieldException{
		return (User1) zhuanghua(obj,new User1(),new Yinshe(){
			@Override
			public Map<String, String> getYinsheGx() {
				Map<String, String> map=new HashMap<String,String>();
				map.put("age", "age");
				map.put("name1", "name");
				return map;
			}
		});
	}
	/**
	 * 集合自转换
	 * @param arrayList
	 * @return
	 * @throws ClassNotFoundException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 * @throws InvocationTargetException
	 * @throws NoSuchMethodException
	 * @throws SecurityException
	 * @throws NoSuchFieldException
	 */
	public static List<User1> zhuanhuaList(List<User> useList) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, NoSuchFieldException{
		List<User1> user1List=new ArrayList<User1>();
		for(User use:useList){
			user1List.add((User1) zhuanghua(use,new User1(),new Yinshe(){
				@Override
				public Map<String, String> getYinsheGx() {
					Map<String, String> map=new HashMap<String,String>();
					map.put("age", "age");
					map.put("name1", "name");
					return map;
				}
			}));//
		}
		return user1List;
	}
	
	
	
	
	/**
	 * 模型自转换底层实现
	 * @param obj
	 * @param obj1
	 * @param YS
	 * @return
	 * @throws ClassNotFoundException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 * @throws InvocationTargetException
	 * @throws NoSuchMethodException
	 * @throws SecurityException
	 * @throws NoSuchFieldException
	 */
	static Object zhuanghua(Object obj,Object obj1,Yinshe YS) throws 
	ClassNotFoundException, 
	InstantiationException, 
	IllegalAccessException,
	IllegalArgumentException,
	InvocationTargetException,
	NoSuchMethodException,
	SecurityException, NoSuchFieldException{
		//获取整个类  
        Class<?> c = Class.forName(obj.getClass().getName());  
        Class<?> c1 = Class.forName(obj1.getClass().getName());  
        @SuppressWarnings("rawtypes")
		java.util.Iterator it = YS.getYinsheGx().entrySet().iterator();
        while(it.hasNext()){
	        @SuppressWarnings("rawtypes")
			java.util.Map.Entry entry = (java.util.Map.Entry)it.next();
	        // 调用TestReflect类中的reflect1方法
	        Method method = c.getMethod(
	        		"get"+captureName((String)entry.getKey()));
	        
	        Field field1 = c1.getDeclaredField((String)entry.getValue());
	        Method method1 = c1.getMethod("set"+captureName(
	        		(String)entry.getValue()),field1.getType());
	     // 调用TestReflect的reflect2方法
	        method1.invoke(obj1, method.invoke(obj));
        }
        return obj1;
    }
	private static String captureName(String name) {
		  //name = name.substring(0, 1).toUpperCase() + name.substring(1);
		  //return  name;
		  char[] cs=name.toCharArray();
		  cs[0]-=32;
		  return String.valueOf(cs);	        
	}
	
	
        // 可以直接对 private 的属性赋值
//        int length=fs.length<fs1.length ? fs.length:fs1.length;
//        for(int i=0;i<length;i++){
//        	// 调用TestReflect类中的reflect1方法
//	        Method method = c.getMethod(
//	        		"get"+captureName(fs[i].getName()));
//	        Method method1 = c1.getMethod("set"+captureName(
//	        		fs1[i].getName()),fs1[i].getType());
//	     // 调用TestReflect的reflect2方法
//	        method1.invoke(obj1, method.invoke(obj));
	        
        
//  Field[] fs = c.getDeclaredFields();
//  Field[] fs1 = c1.getDeclaredFields();	        
//        	fs1[i].setAccessible(true);
//        	fs[i].setAccessible(true);
//        	fs1[i].set(obj1, fs[i].get(obj));
//        	fs1[i].setAccessible(false);
//        	fs[i].setAccessible(false);
}



接口的使用达到了即用即设的效果,zhuanghua()方法是一个底层方法,本人认为可以存放在,模型包中设为包级访问权限。功能:实现了通用的在规定的映射关系下,将两个模型互相转换。

zhuanhuaObject方法的功能:定义模型转化的映射关系,可将任何其他类型的模型转化为this对象,实现时机:在你需要将任意一个模型转化为本模型时可将此方法的实现写在改模型类中,并且实现映射关系
zhuanhuaList方法的功能:定义模型转化的映射关系,可将两个给定的list类型的数据集合转换,实现时机:在你需要将任意一个模型转化为本模型时可将此方法的实现写在改模型类中,并且实现映射关系
    
    
zhuanhuaList的完善:达到可将任何其他类型的list集合数据转化为某对象的lit集合。





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TensorRT是一种用于高性能深度学习推理的优化器。它可以针对特定硬件进行优化,提高推理速度和性能。TensorRT支持多种深度学习框架,如TensorFlow、Caffe、PyTorch等。 TensorRT模型转化是将训练好的深度学习模型转化为TensorRT可读取的格式。这个过程可以通过TensorRT提供的Python API来完成。 以下是一个简单的TensorFlow模型转化为TensorRT模型的示例: ```python import tensorflow as tf import tensorrt as trt # 加载TensorFlow模型 with tf.Session() as sess: saver = tf.train.import_meta_graph('model.ckpt.meta') saver.restore(sess, 'model.ckpt') graph_def = tf.graph_util.convert_variables_to_constants(sess, sess.graph_def, ['output']) # 创建TensorRT builder和network builder = trt.Builder(TRT_LOGGER) network = builder.create_network() # 创建TensorRT的输入和输出张量 input_tensor = network.add_input('input', trt.float32, [-1, 224, 224, 3]) output_tensor = network.add_output('output', trt.float32, [-1, 1000]) # 将TensorFlow的图转化为TensorRT的图 parser = trt.UffParser() parser.register_input('input', (3, 224, 224)) parser.register_output('output') parser.parse('model.uff', network) # 编译TensorRT模型 engine = builder.build_cuda_engine(network) # 保存TensorRT模型 with open('model.trt', 'wb') as f: f.write(engine.serialize()) ``` 在上面的示例中,我们首先加载TensorFlow模型,然后创建TensorRT builder和network。接下来,我们创建TensorRT的输入和输出张量,并将TensorFlow的图转化为TensorRT的图。最后,我们编译TensorRT模型并将其保存到文件中。 需要注意的是,TensorRT模型转化的过程可能因为多种原因失败,例如TensorFlow模型中使用了不支持的操作、输入输出张量的维度不正确等。因此,在进行TensorRT模型转化时需要仔细检查模型的结构和输入输出张量的维度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值